Search in sources :

Example 11 with LocalResult

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

the class SelectUnion method queryWithoutCache.

@Override
protected ResultInterface queryWithoutCache(int maxRows, ResultTarget target) {
    if (maxRows != 0) {
        // maxRows is set (maxRows 0 means no limit)
        int l;
        if (limitExpr == null) {
            l = -1;
        } else {
            Value v = limitExpr.getValue(session);
            l = v == ValueNull.INSTANCE ? -1 : v.getInt();
        }
        if (l < 0) {
            // for limitExpr, 0 means no rows, and -1 means no limit
            l = maxRows;
        } else {
            l = Math.min(l, maxRows);
        }
        limitExpr = ValueExpression.get(ValueInt.get(l));
    }
    if (session.getDatabase().getSettings().optimizeInsertFromSelect) {
        if (unionType == UnionType.UNION_ALL && target != null) {
            if (sort == null && !distinct && maxRows == 0 && offsetExpr == null && limitExpr == null) {
                left.query(0, target);
                right.query(0, target);
                return null;
            }
        }
    }
    int columnCount = left.getColumnCount();
    if (session.isLazyQueryExecution() && unionType == UnionType.UNION_ALL && !distinct && sort == null && !randomAccessResult && !isForUpdate && offsetExpr == null && isReadOnly()) {
        int limit = -1;
        if (limitExpr != null) {
            Value v = limitExpr.getValue(session);
            if (v != ValueNull.INSTANCE) {
                limit = v.getInt();
            }
        }
        // limit 0 means no rows
        if (limit != 0) {
            LazyResultUnion lazyResult = new LazyResultUnion(expressionArray, columnCount);
            if (limit > 0) {
                lazyResult.setLimit(limit);
            }
            return lazyResult;
        }
    }
    LocalResult result = new LocalResult(session, expressionArray, columnCount);
    if (sort != null) {
        result.setSortOrder(sort);
    }
    if (distinct) {
        left.setDistinct(true);
        right.setDistinct(true);
        result.setDistinct();
    }
    if (randomAccessResult) {
        result.setRandomAccess();
    }
    switch(unionType) {
        case UNION:
        case EXCEPT:
            left.setDistinct(true);
            right.setDistinct(true);
            result.setDistinct();
            break;
        case UNION_ALL:
            break;
        case INTERSECT:
            left.setDistinct(true);
            right.setDistinct(true);
            break;
        default:
            DbException.throwInternalError("type=" + unionType);
    }
    ResultInterface l = left.query(0);
    ResultInterface r = right.query(0);
    l.reset();
    r.reset();
    switch(unionType) {
        case UNION_ALL:
        case UNION:
            {
                while (l.next()) {
                    result.addRow(convert(l.currentRow(), columnCount));
                }
                while (r.next()) {
                    result.addRow(convert(r.currentRow(), columnCount));
                }
                break;
            }
        case EXCEPT:
            {
                while (l.next()) {
                    result.addRow(convert(l.currentRow(), columnCount));
                }
                while (r.next()) {
                    result.removeDistinct(convert(r.currentRow(), columnCount));
                }
                break;
            }
        case INTERSECT:
            {
                LocalResult temp = new LocalResult(session, expressionArray, columnCount);
                temp.setDistinct();
                temp.setRandomAccess();
                while (l.next()) {
                    temp.addRow(convert(l.currentRow(), columnCount));
                }
                while (r.next()) {
                    Value[] values = convert(r.currentRow(), columnCount);
                    if (temp.containsDistinct(values)) {
                        result.addRow(values);
                    }
                }
                temp.close();
                break;
            }
        default:
            DbException.throwInternalError("type=" + unionType);
    }
    if (offsetExpr != null) {
        result.setOffset(offsetExpr.getValue(session).getInt());
    }
    if (limitExpr != null) {
        Value v = limitExpr.getValue(session);
        if (v != ValueNull.INSTANCE) {
            result.setLimit(v.getInt());
        }
    }
    l.close();
    r.close();
    result.done();
    if (target != null) {
        while (result.next()) {
            target.addRow(result.currentRow());
        }
        result.close();
        return null;
    }
    return result;
}
Also used : LocalResult(org.h2.result.LocalResult) ResultInterface(org.h2.result.ResultInterface) Value(org.h2.value.Value)

Example 12 with LocalResult

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

the class Call method query.

@Override
public ResultInterface query(int maxrows) {
    setCurrentRowNumber(1);
    Value v = expression.getValue(session);
    if (isResultSet) {
        v = v.convertTo(Value.RESULT_SET);
        ResultSet rs = v.getResultSet();
        return LocalResult.read(session, rs, maxrows);
    }
    LocalResult result = new LocalResult(session, expressions, 1);
    Value[] row = { v };
    result.addRow(row);
    result.done();
    return result;
}
Also used : LocalResult(org.h2.result.LocalResult) Value(org.h2.value.Value) ResultSet(java.sql.ResultSet)

Example 13 with LocalResult

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

the class FunctionTable method getResult.

/**
 * Read the result from the function. This method buffers the result in a
 * temporary file.
 *
 * @param session the session
 * @return the result
 */
public ResultInterface getResult(Session session) {
    ValueResultSet v = getValueResultSet(session);
    if (v == null) {
        return null;
    }
    if (cachedResult != null && cachedValue == v) {
        cachedResult.reset();
        return cachedResult;
    }
    LocalResult result = LocalResult.read(session, v.getResultSet(), 0);
    if (function.isDeterministic()) {
        cachedResult = result;
        cachedValue = v;
    }
    return result;
}
Also used : LocalResult(org.h2.result.LocalResult) ValueResultSet(org.h2.value.ValueResultSet)

Example 14 with LocalResult

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

the class TableFunction method getSimpleResultSet.

private static SimpleResultSet getSimpleResultSet(LocalResult rs, int maxrows) {
    int columnCount = rs.getVisibleColumnCount();
    SimpleResultSet simple = new SimpleResultSet();
    simple.setAutoClose(false);
    for (int i = 0; i < columnCount; i++) {
        String name = rs.getColumnName(i);
        DataType dataType = DataType.getDataType(rs.getColumnType(i));
        int sqlType = dataType.sqlType;
        String sqlTypeName = dataType.name;
        int precision = MathUtils.convertLongToInt(rs.getColumnPrecision(i));
        int scale = rs.getColumnScale(i);
        simple.addColumn(name, sqlType, sqlTypeName, precision, scale);
    }
    rs.reset();
    for (int i = 0; i < maxrows && rs.next(); i++) {
        Object[] list = new Object[columnCount];
        for (int j = 0; j < columnCount; j++) {
            list[j] = rs.currentRow()[j].getObject();
        }
        simple.addRow(list);
    }
    return simple;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) DataType(org.h2.value.DataType)

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