Search in sources :

Example 6 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class DropAggregate method update.

@Override
public int update() {
    session.getUser().checkAdmin();
    session.commit(true);
    Database db = session.getDatabase();
    UserAggregate aggregate = db.findAggregate(name);
    if (aggregate == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.AGGREGATE_NOT_FOUND_1, name);
        }
    } else {
        db.removeDatabaseObject(session, aggregate);
    }
    return 0;
}
Also used : UserAggregate(org.h2.engine.UserAggregate) Database(org.h2.engine.Database)

Example 7 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class AggregateDataMedian method getMedianColumnIndex.

/**
 * Get the index (if any) for the column specified in the median aggregate.
 *
 * @param on the expression (usually a column expression)
 * @return the index, or null
 */
static Index getMedianColumnIndex(Expression on) {
    if (on instanceof ExpressionColumn) {
        ExpressionColumn col = (ExpressionColumn) on;
        Column column = col.getColumn();
        TableFilter filter = col.getTableFilter();
        if (filter != null) {
            Table table = filter.getTable();
            ArrayList<Index> indexes = table.getIndexes();
            Index result = null;
            if (indexes != null) {
                boolean nullable = column.isNullable();
                for (int i = 1, size = indexes.size(); i < size; i++) {
                    Index index = indexes.get(i);
                    if (!index.canFindNext()) {
                        continue;
                    }
                    if (!index.isFirstColumn(column)) {
                        continue;
                    }
                    // Prefer index without nulls last for nullable columns
                    if (result == null || result.getColumns().length > index.getColumns().length || nullable && isNullsLast(result) && !isNullsLast(index)) {
                        result = index;
                    }
                }
            }
            return result;
        }
    }
    return null;
}
Also used : Table(org.h2.table.Table) Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) TableFilter(org.h2.table.TableFilter) Index(org.h2.index.Index)

Example 8 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class JavaAggregate method updateAggregate.

@Override
public void updateAggregate(Session session) {
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
        // this is a different level (the enclosing query)
        return;
    }
    int groupRowId = select.getCurrentGroupRowId();
    if (lastGroupRowId == groupRowId) {
        // already visited
        return;
    }
    lastGroupRowId = groupRowId;
    if (filterCondition != null) {
        if (!filterCondition.getBooleanValue(session)) {
            return;
        }
    }
    Aggregate agg = (Aggregate) group.get(this);
    try {
        if (agg == null) {
            agg = getInstance();
            group.put(this, agg);
        }
        Object[] argValues = new Object[args.length];
        Object arg = null;
        for (int i = 0, len = args.length; i < len; i++) {
            Value v = args[i].getValue(session);
            v = v.convertTo(argTypes[i]);
            arg = v.getObject();
            argValues[i] = arg;
        }
        if (args.length == 1) {
            agg.add(arg);
        } else {
            agg.add(argValues);
        }
    } catch (SQLException e) {
        throw DbException.convert(e);
    }
}
Also used : SQLException(java.sql.SQLException) Value(org.h2.value.Value) UserAggregate(org.h2.engine.UserAggregate) Aggregate(org.h2.api.Aggregate)

Example 9 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class TestFunctions method testAggregate.

private void testAggregate() throws SQLException {
    deleteDb("functions");
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    stat.execute("CREATE AGGREGATE SIMPLE_MEDIAN FOR \"" + MedianString.class.getName() + "\"");
    stat.execute("CREATE AGGREGATE IF NOT EXISTS SIMPLE_MEDIAN FOR \"" + MedianString.class.getName() + "\"");
    ResultSet rs = stat.executeQuery("SELECT SIMPLE_MEDIAN(X) FROM SYSTEM_RANGE(1, 9)");
    rs.next();
    assertEquals("5", rs.getString(1));
    conn.close();
    if (config.memory) {
        return;
    }
    conn = getConnection("functions");
    stat = conn.createStatement();
    stat.executeQuery("SELECT SIMPLE_MEDIAN(X) FROM SYSTEM_RANGE(1, 9)");
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getProcedures(null, null, "SIMPLE_MEDIAN");
    assertTrue(rs.next());
    assertFalse(rs.next());
    rs = stat.executeQuery("SCRIPT");
    boolean found = false;
    while (rs.next()) {
        String sql = rs.getString(1);
        if (sql.contains("SIMPLE_MEDIAN")) {
            found = true;
        }
    }
    assertTrue(found);
    stat.execute("DROP AGGREGATE SIMPLE_MEDIAN");
    stat.execute("DROP AGGREGATE IF EXISTS SIMPLE_MEDIAN");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 10 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class TestFunctions method testPrecision.

private void testPrecision() throws SQLException {
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    stat.execute("create alias no_op for \"" + getClass().getName() + ".noOp\"");
    PreparedStatement prep = conn.prepareStatement("select * from dual where no_op(1.6)=?");
    prep.setBigDecimal(1, new BigDecimal("1.6"));
    ResultSet rs = prep.executeQuery();
    assertTrue(rs.next());
    stat.execute("create aggregate agg_sum for \"" + getClass().getName() + "\"");
    rs = stat.executeQuery("select agg_sum(1), sum(1.6) from dual");
    rs.next();
    assertEquals(1, rs.getMetaData().getScale(2));
    assertEquals(32767, rs.getMetaData().getScale(1));
    stat.executeQuery("select * from information_schema.function_aliases");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Aggregations

UserAggregate (org.h2.engine.UserAggregate)8 Expression (org.h2.expression.Expression)6 ValueExpression (org.h2.expression.ValueExpression)6 Aggregate (org.h2.api.Aggregate)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 Statement (java.sql.Statement)4 SimpleResultSet (org.h2.tools.SimpleResultSet)4 ValueString (org.h2.value.ValueString)4 CallableStatement (java.sql.CallableStatement)3 PreparedStatement (java.sql.PreparedStatement)3 ConditionInSelect (org.h2.expression.ConditionInSelect)3 Column (org.h2.table.Column)3 IndexColumn (org.h2.table.IndexColumn)3 DatabaseMetaData (java.sql.DatabaseMetaData)2 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)2 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)2 DropFunctionAlias (org.h2.command.ddl.DropFunctionAlias)2 DropSchema (org.h2.command.ddl.DropSchema)2