Search in sources :

Example 6 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

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();
    ValueResultSet vr = ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
    return vr;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) Column(com.wplatform.ddal.dbobject.table.Column) Database(com.wplatform.ddal.engine.Database)

Example 7 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

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(com.wplatform.ddal.result.LocalResult) ValueResultSet(com.wplatform.ddal.value.ValueResultSet)

Example 8 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

the class SelectUnion method queryWithoutCache.

@Override
protected LocalResult 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 == 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();
    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);
    }
    LocalResult l = left.query(0);
    LocalResult 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);
                    }
                }
                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(com.wplatform.ddal.result.LocalResult) Value(com.wplatform.ddal.value.Value)

Example 9 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

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(com.wplatform.ddal.result.LocalResult)

Example 10 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

the class Call method queryMeta.

@Override
public ResultInterface queryMeta() {
    LocalResult result;
    if (isResultSet) {
        Expression[] expr = expression.getExpressionColumns(session);
        result = new LocalResult(session, expr, expr.length);
    } else {
        result = new LocalResult(session, expressions, 1);
    }
    result.done();
    return result;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) Expression(com.wplatform.ddal.command.expression.Expression)

Aggregations

LocalResult (com.wplatform.ddal.result.LocalResult)14 Value (com.wplatform.ddal.value.Value)6 Expression (com.wplatform.ddal.command.expression.Expression)2 Column (com.wplatform.ddal.dbobject.table.Column)2 Database (com.wplatform.ddal.engine.Database)2 ExpressionColumn (com.wplatform.ddal.command.expression.ExpressionColumn)1 Session (com.wplatform.ddal.engine.Session)1 ResultTarget (com.wplatform.ddal.result.ResultTarget)1 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)1 ValueResultSet (com.wplatform.ddal.value.ValueResultSet)1 ValueString (com.wplatform.ddal.value.ValueString)1 ResultSet (java.sql.ResultSet)1