Search in sources :

Example 6 with LocalResult

use of org.h2.result.LocalResult in project h2database by h2database.

the class TableFunction method getTable.

private ValueResultSet getTable(Session session, Expression[] argList, boolean onlyColumnList, boolean distinctRows) {
    int len = columnList.length;
    Expression[] header = new Expression[len];
    Database db = session.getDatabase();
    for (int i = 0; i < len; i++) {
        Column c = columnList[i];
        ExpressionColumn col = new ExpressionColumn(db, c);
        header[i] = col;
    }
    LocalResult result = new LocalResult(session, header, len);
    if (distinctRows) {
        result.setDistinct();
    }
    if (!onlyColumnList) {
        Value[][] list = new Value[len][];
        int rows = 0;
        for (int i = 0; i < len; i++) {
            Value v = argList[i].getValue(session);
            if (v == ValueNull.INSTANCE) {
                list[i] = new Value[0];
            } else {
                ValueArray array = (ValueArray) v.convertTo(Value.ARRAY);
                Value[] l = array.getList();
                list[i] = l;
                rows = Math.max(rows, l.length);
            }
        }
        for (int row = 0; row < rows; row++) {
            Value[] r = new Value[len];
            for (int j = 0; j < len; j++) {
                Value[] l = list[j];
                Value v;
                if (l.length <= row) {
                    v = ValueNull.INSTANCE;
                } else {
                    Column c = columnList[j];
                    v = l[row];
                    v = c.convert(v);
                    v = v.convertPrecision(c.getPrecision(), false);
                    v = v.convertScale(true, c.getScale());
                }
                r[j] = v;
            }
            result.addRow(r);
        }
    }
    result.done();
    return ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
}
Also used : LocalResult(org.h2.result.LocalResult) Column(org.h2.table.Column) Database(org.h2.engine.Database) Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 7 with LocalResult

use of org.h2.result.LocalResult in project h2database by h2database.

the class ViewIndex method findRecursive.

private Cursor findRecursive(SearchRow first, SearchRow last) {
    assert recursive;
    ResultInterface recursiveResult = view.getRecursiveResult();
    if (recursiveResult != null) {
        recursiveResult.reset();
        return new ViewCursor(this, recursiveResult, first, last);
    }
    if (query == null) {
        Parser parser = new Parser(createSession);
        parser.setRightsChecked(true);
        parser.setSuppliedParameterList(originalParameters);
        query = (Query) parser.prepare(querySQL);
        query.setNeverLazy(true);
    }
    if (!query.isUnion()) {
        throw DbException.get(ErrorCode.SYNTAX_ERROR_2, "recursive queries without UNION");
    }
    SelectUnion union = (SelectUnion) query;
    Query left = union.getLeft();
    left.setNeverLazy(true);
    // to ensure the last result is not closed
    left.disableCache();
    ResultInterface resultInterface = left.query(0);
    LocalResult localResult = union.getEmptyResult();
    // ensure it is not written to disk,
    // because it is not closed normally
    localResult.setMaxMemoryRows(Integer.MAX_VALUE);
    while (resultInterface.next()) {
        Value[] cr = resultInterface.currentRow();
        localResult.addRow(cr);
    }
    Query right = union.getRight();
    right.setNeverLazy(true);
    resultInterface.reset();
    view.setRecursiveResult(resultInterface);
    // to ensure the last result is not closed
    right.disableCache();
    while (true) {
        resultInterface = right.query(0);
        if (!resultInterface.hasNext()) {
            break;
        }
        while (resultInterface.next()) {
            Value[] cr = resultInterface.currentRow();
            localResult.addRow(cr);
        }
        resultInterface.reset();
        view.setRecursiveResult(resultInterface);
    }
    view.setRecursiveResult(null);
    localResult.done();
    return new ViewCursor(this, localResult, first, last);
}
Also used : LocalResult(org.h2.result.LocalResult) SelectUnion(org.h2.command.dml.SelectUnion) ResultInterface(org.h2.result.ResultInterface) Query(org.h2.command.dml.Query) Value(org.h2.value.Value) Parser(org.h2.command.Parser)

Example 8 with LocalResult

use of org.h2.result.LocalResult in project h2database by h2database.

the class ScriptCommand method queryMeta.

@Override
public ResultInterface queryMeta() {
    LocalResult r = createResult();
    r.done();
    return r;
}
Also used : LocalResult(org.h2.result.LocalResult)

Example 9 with LocalResult

use of org.h2.result.LocalResult in project h2database by h2database.

the class Select method queryGroup.

private void queryGroup(int columnCount, LocalResult result) {
    ValueHashMap<HashMap<Expression, Object>> groups = ValueHashMap.newInstance();
    int rowNumber = 0;
    setCurrentRowNumber(0);
    currentGroup = null;
    ValueArray defaultGroup = ValueArray.get(new Value[0]);
    int sampleSize = getSampleSizeValue(session);
    while (topTableFilter.next()) {
        setCurrentRowNumber(rowNumber + 1);
        if (isConditionMet()) {
            Value key;
            rowNumber++;
            if (groupIndex == null) {
                key = defaultGroup;
            } else {
                Value[] keyValues = new Value[groupIndex.length];
                // update group
                for (int i = 0; i < groupIndex.length; i++) {
                    int idx = groupIndex[i];
                    Expression expr = expressions.get(idx);
                    keyValues[i] = expr.getValue(session);
                }
                key = ValueArray.get(keyValues);
            }
            HashMap<Expression, Object> values = groups.get(key);
            if (values == null) {
                values = new HashMap<>();
                groups.put(key, values);
            }
            currentGroup = values;
            currentGroupRowId++;
            for (int i = 0; i < columnCount; i++) {
                if (groupByExpression == null || !groupByExpression[i]) {
                    Expression expr = expressions.get(i);
                    expr.updateAggregate(session);
                }
            }
            if (sampleSize > 0 && rowNumber >= sampleSize) {
                break;
            }
        }
    }
    if (groupIndex == null && groups.size() == 0) {
        groups.put(defaultGroup, new HashMap<Expression, Object>());
    }
    ArrayList<Value> keys = groups.keys();
    for (Value v : keys) {
        ValueArray key = (ValueArray) v;
        currentGroup = groups.get(key);
        Value[] keyValues = key.getList();
        Value[] row = new Value[columnCount];
        for (int j = 0; groupIndex != null && j < groupIndex.length; j++) {
            row[groupIndex[j]] = keyValues[j];
        }
        for (int j = 0; j < columnCount; j++) {
            if (groupByExpression != null && groupByExpression[j]) {
                continue;
            }
            Expression expr = expressions.get(j);
            row[j] = expr.getValue(session);
        }
        if (isHavingNullOrFalse(row)) {
            continue;
        }
        row = keepOnlyDistinct(row, columnCount);
        result.addRow(row);
    }
}
Also used : HashMap(java.util.HashMap) Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 10 with LocalResult

use of org.h2.result.LocalResult in project h2database by h2database.

the class SelectUnion method queryMeta.

@Override
public ResultInterface queryMeta() {
    int columnCount = left.getColumnCount();
    LocalResult result = new LocalResult(session, expressionArray, columnCount);
    result.done();
    return result;
}
Also used : LocalResult(org.h2.result.LocalResult)

Aggregations

LocalResult (org.h2.result.LocalResult)11 Value (org.h2.value.Value)7 Database (org.h2.engine.Database)3 Expression (org.h2.expression.Expression)3 Column (org.h2.table.Column)3 HashMap (java.util.HashMap)2 ExpressionColumn (org.h2.expression.ExpressionColumn)2 ResultInterface (org.h2.result.ResultInterface)2 ValueArray (org.h2.value.ValueArray)2 ValueString (org.h2.value.ValueString)2 IOException (java.io.IOException)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Parser (org.h2.command.Parser)1 Query (org.h2.command.dml.Query)1 SelectUnion (org.h2.command.dml.SelectUnion)1 Constraint (org.h2.constraint.Constraint)1 Comment (org.h2.engine.Comment)1 DbObject (org.h2.engine.DbObject)1