Search in sources :

Example 46 with TimestampType

use of org.voltdb.types.TimestampType in project voltdb by VoltDB.

the class TestSQLTypesSuite method testJumboRow.

public void testJumboRow() throws Exception {
    final Client client = getClient();
    byte[] firstStringBytes = new byte[1048576];
    java.util.Arrays.fill(firstStringBytes, (byte) 'c');
    String firstString = new String(firstStringBytes, "UTF-8");
    byte[] secondStringBytes = new byte[1048564];
    java.util.Arrays.fill(secondStringBytes, (byte) 'a');
    String secondString = new String(secondStringBytes, "UTF-8");
    Object[] params = new Object[] { "JUMBO_ROW", 0, 0, 0, 0, 0, 0.0, new TimestampType(0), firstString, secondString, "", "", new byte[0], new byte[0], VoltType.NULL_DECIMAL, null, null };
    VoltTable[] results = client.callProcedure("Insert", params).getResults();
    params = null;
    firstString = null;
    secondString = null;
    assertEquals(results.length, 1);
    assertEquals(1, results[0].asScalarLong());
    results = client.callProcedure("Select", "JUMBO_ROW", 0).getResults();
    assertEquals(results.length, 1);
    assertTrue(results[0].advanceRow());
    assertTrue(java.util.Arrays.equals(results[0].getStringAsBytes(1), firstStringBytes));
    assertTrue(java.util.Arrays.equals(results[0].getStringAsBytes(2), secondStringBytes));
    java.util.Arrays.fill(firstStringBytes, (byte) 'q');
    firstString = new String(firstStringBytes, "UTF-8");
    java.util.Arrays.fill(secondStringBytes, (byte) 'r');
    secondString = new String(secondStringBytes, "UTF-8");
    params = new Object[] { "JUMBO_ROW", 0, 0, 0, 0, 0, 0.0, new TimestampType(0), firstString, secondString, "", "", new byte[0], new byte[0], VoltType.NULL_DECIMAL, null, null };
    results = client.callProcedure("Update", params).getResults();
    params = null;
    firstString = null;
    secondString = null;
    assertEquals(results.length, 1);
    assertEquals(1, results[0].asScalarLong());
    results = client.callProcedure("Select", "JUMBO_ROW", 0).getResults();
    assertEquals(results.length, 1);
    assertTrue(results[0].advanceRow());
    assertTrue(java.util.Arrays.equals(results[0].getStringAsBytes(1), firstStringBytes));
    assertTrue(java.util.Arrays.equals(results[0].getStringAsBytes(2), secondStringBytes));
}
Also used : TimestampType(org.voltdb.types.TimestampType) Client(org.voltdb.client.Client) VoltTable(org.voltdb.VoltTable)

Example 47 with TimestampType

use of org.voltdb.types.TimestampType in project voltdb by VoltDB.

the class TestSQLTypesSuite method comparisonHelper.

/** Utility to compare two instances of a VoltType for equality */
@SuppressWarnings({ "incomplete-switch" })
private boolean comparisonHelper(final Object lhs, final Object rhs, final VoltType vt) {
    switch(vt) {
        case TINYINT:
            final Byte b1 = (Byte) lhs;
            final Byte b2 = (Byte) rhs;
            // System.out.println("\tComparing " + b1 + " == " + b2);
            return b1.byteValue() == b2.byteValue();
        case SMALLINT:
            final Short s1 = (Short) lhs;
            final Short s2 = (Short) rhs;
            // System.out.println("\tComparing " + s1 + " == " + s2);
            return s1.shortValue() == s2.shortValue();
        case INTEGER:
            final Integer i1 = (Integer) lhs;
            final Integer i2 = (Integer) rhs;
            // System.out.println("\tComparing " + i1 + " == " + i2);
            return i1.intValue() == i2.intValue();
        case BIGINT:
            final Long l1 = (Long) lhs;
            final Long l2 = (Long) rhs;
            // System.out.println("\tComparing " + l1 + " == " + l2);
            return l1.longValue() == l2.longValue();
        case FLOAT:
            final Double d1 = (Double) lhs;
            final Double d2 = (Double) rhs;
            // Handle the screwy null double value (isn't quite min double)
            if (((d1 == VoltType.NULL_FLOAT) && (d2 <= d1)) || ((d2 == VoltType.NULL_FLOAT) && (d1 <= d2))) {
                return true;
            }
            return (Math.abs(d1 - d2) < 0.0000000001);
        case STRING:
            // System.out.println("\tComparing " + lhs + " == " + rhs);
            if ((lhs == null || lhs == VoltType.NULL_STRING_OR_VARBINARY) && (rhs == null || rhs == VoltType.NULL_STRING_OR_VARBINARY)) {
                return true;
            }
            return ((String) lhs).equals(rhs);
        case VARBINARY:
            boolean lhsnull = (lhs == null || lhs == VoltType.NULL_STRING_OR_VARBINARY);
            boolean rhsnull = (rhs == null || rhs == VoltType.NULL_STRING_OR_VARBINARY);
            if (lhsnull && rhsnull)
                return true;
            if (lhsnull != rhsnull)
                return false;
            // assume neither is null from here
            String lhs2 = null;
            String rhs2 = null;
            if (lhs instanceof byte[])
                lhs2 = Encoder.hexEncode((byte[]) lhs);
            else
                lhs2 = (String) lhs;
            if (rhs instanceof byte[])
                rhs2 = Encoder.hexEncode((byte[]) rhs);
            else
                rhs2 = (String) rhs;
            return lhs2.equalsIgnoreCase(rhs2);
        case TIMESTAMP:
            // System.out.println("\tComparing " + lhs + " == " + rhs);
            if ((lhs == null || lhs == VoltType.NULL_TIMESTAMP) && (rhs == null || rhs == VoltType.NULL_TIMESTAMP)) {
                return true;
            }
            return ((TimestampType) lhs).equals(rhs);
        case DECIMAL:
            // System.out.println("\tComparing " + lhs + " == " + rhs);
            if ((lhs == null || lhs == VoltType.NULL_DECIMAL) && (rhs == null || rhs == VoltType.NULL_DECIMAL)) {
                return true;
            }
            return ((BigDecimal) lhs).equals(rhs);
        case GEOGRAPHY_POINT:
            {
                if ((lhs == VoltType.NULL_POINT || lhs == null) && (rhs == VoltType.NULL_POINT || rhs == null)) {
                    return true;
                }
                GeographyPointValue gpvLhs = (GeographyPointValue) lhs;
                GeographyPointValue gpvRhs = (GeographyPointValue) rhs;
                return gpvLhs.equals(gpvRhs);
            }
        case GEOGRAPHY:
            {
                if ((lhs == VoltType.NULL_GEOGRAPHY || lhs == null) && (rhs == VoltType.NULL_GEOGRAPHY || rhs == null)) {
                    return true;
                }
                GeographyValue gvLhs = (GeographyValue) lhs;
                GeographyValue gvRhs = (GeographyValue) rhs;
                return gvLhs.equals(gvRhs);
            }
        default:
            throw new IllegalArgumentException("Unknown type in comparisonHelper");
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) GeographyValue(org.voltdb.types.GeographyValue) TimestampType(org.voltdb.types.TimestampType) GeographyPointValue(org.voltdb.types.GeographyPointValue) BigDecimal(java.math.BigDecimal)

Example 48 with TimestampType

use of org.voltdb.types.TimestampType in project voltdb by VoltDB.

the class TestSQLTypesSuite method testPassingDateAndTimeObjectsBeforeEpochToStatements.

public void testPassingDateAndTimeObjectsBeforeEpochToStatements() throws Exception {
    final Client client = this.getClient();
    Random rn = new Random();
    for (int i = 0; i < 100; ++i) {
        long ts = rn.nextLong();
        long[] timestampValues = { // random
        ts, // even milliseconds
        ts / 1000 * 1000, // even seconds
        ts / 1000000 * 1000000, // even seconds
        ts / 1000000 * -1000000 };
        for (long microsecondsSinceEpoch : timestampValues) {
            TimestampType tst_micro = new TimestampType(microsecondsSinceEpoch);
            // Add 1 more millis from epoch
            java.sql.Timestamp ts_micro = VoltTypeUtil.getSqlTimestampFromMicrosSinceEpoch(microsecondsSinceEpoch);
            client.callProcedure("Insert", "ALLOW_NULLS", 0, 0, 0, 0, 0, null, tst_micro, null, null, null, null, null, null, null, null, null);
            VoltTable vt;
            vt = client.callProcedure("@AdHoc", "Select A_TIMESTAMP from allow_nulls where pkey = 0").getResults()[0];
            assertTrue(vt.advanceRow());
            assertEquals(microsecondsSinceEpoch, vt.getTimestampAsLong(0));
            assertEquals(tst_micro, vt.getTimestampAsTimestamp(0));
            assertEquals(ts_micro, vt.getTimestampAsSqlTimestamp(0));
            client.callProcedure("@AdHoc", "truncate table allow_nulls;");
        }
    }
}
Also used : Random(java.util.Random) TimestampType(org.voltdb.types.TimestampType) Client(org.voltdb.client.Client) VoltTable(org.voltdb.VoltTable)

Example 49 with TimestampType

use of org.voltdb.types.TimestampType in project voltdb by VoltDB.

the class TestWindowFunctionSuite method subtestRankWithTimestamp.

private void subtestRankWithTimestamp() throws Exception {
    Client client = getClient();
    long baseTime = TimestampType.millisFromJDBCformat("1953-06-10 00:00:00");
    Long[][] input = expected.clone();
    shuffleArray(input);
    ClientResponse cr;
    VoltTable vt;
    for (Long[] row : input) {
        cr = client.callProcedure("T_TIMESTAMP.insert", row[colA], row[colB], new TimestampType(baseTime + row[colB] * 1000));
        assertEquals(ClientResponse.SUCCESS, cr.getStatus());
    }
    String sql = "select A, B, C, rank() over (partition by A order by C) as R from T_TIMESTAMP ORDER BY A, B, C, R;";
    cr = client.callProcedure("@AdHoc", sql);
    assertEquals(ClientResponse.SUCCESS, cr.getStatus());
    vt = cr.getResults()[0];
    assertEquals(expected.length, vt.getRowCount());
    for (int rowIdx = 0; vt.advanceRow(); rowIdx += 1) {
        String msg = String.format("Row %d:", rowIdx);
        assertEquals(msg, expected[rowIdx][colA], Long.valueOf(vt.getLong(0)));
        assertEquals(msg, expected[rowIdx][colB], Long.valueOf(vt.getLong(1)));
        assertEquals(msg, baseTime + expected[rowIdx][colB] * 1000, vt.getTimestampAsLong(2));
        assertEquals(msg, expected[rowIdx][colR_A], Long.valueOf(vt.getLong(3)));
    }
}
Also used : ClientResponse(org.voltdb.client.ClientResponse) TimestampType(org.voltdb.types.TimestampType) Client(org.voltdb.client.Client) VoltTable(org.voltdb.VoltTable)

Example 50 with TimestampType

use of org.voltdb.types.TimestampType in project voltdb by VoltDB.

the class TestWindowFunctionSuite method subtestCount.

private void subtestCount() throws Exception {
    Client client = getClient();
    initUniqueTable(client);
    // Try some simple cases with partition by and order by.
    validateCount(client, "select a, count(*) over (partition by a) from tu order by a", new long[][] { { 10, 1 }, { 20, 1 }, { 30, 1 }, { 40, 1 }, { 50, 1 } });
    validateCount(client, "select a, count(*) over (order by a) from tu order by a", new long[][] { { 10, 1 }, { 20, 2 }, { 30, 3 }, { 40, 4 }, { 50, 5 } });
    validateCount(client, "select a, count(*) over () from tu order by a", new long[][] { { 10, 5 }, { 20, 5 }, { 30, 5 }, { 40, 5 }, { 50, 5 } });
    // Try some simple cases with partition by and order by
    // on tables that have duplicate order keys.
    client.callProcedure("tm.insert", 10, 1);
    client.callProcedure("tm.insert", 10, 1);
    client.callProcedure("tm.insert", 10, 2);
    client.callProcedure("tm.insert", 20, 1);
    client.callProcedure("tm.insert", 30, 3);
    client.callProcedure("tm.insert", 30, 1);
    client.callProcedure("tm.insert", 40, 2);
    client.callProcedure("tm.insert", 40, 3);
    client.callProcedure("tm.insert", 50, 2);
    validateCount(client, "select a, count(*) over (partition by a) from tm order by a", new long[][] { { 10, 3 }, { 10, 3 }, { 10, 3 }, { 20, 1 }, { 30, 2 }, { 30, 2 }, { 40, 2 }, { 40, 2 }, { 50, 1 } });
    validateCount(client, "select a, count(*) over (partition by a order by b) from tm order by a", new long[][] { { 10, 2 }, { 10, 2 }, { 10, 3 }, { 20, 1 }, { 30, 1 }, { 30, 2 }, { 40, 1 }, { 40, 2 }, { 50, 1 } });
    validateCount(client, "select a, count(*) over (order by a) from tm order by a", new long[][] { { 10, 3 }, { 10, 3 }, { 10, 3 }, { 20, 4 }, { 30, 6 }, { 30, 6 }, { 40, 8 }, { 40, 8 }, { 50, 9 } });
    validateCount(client, "select a, count(*) over () from tm order by a", new long[][] { { 10, 9 }, { 10, 9 }, { 10, 9 }, { 20, 9 }, { 30, 9 }, { 30, 9 }, { 40, 9 }, { 40, 9 }, { 50, 9 } });
    client.callProcedure("@AdHoc", "truncate table tm");
    client.callProcedure("@AdHoc", "truncate table tu");
    // Try some cases with nulls.  These answers are not
    // the same as the answers we would see with postsgresql,
    // since we sort nulls first and PG sorts them last.
    client.callProcedure("t.insert", 1, 1, 100);
    client.callProcedure("t.insert", 1, 2, 101);
    client.callProcedure("t.insert", 1, 3, 102);
    client.callProcedure("t.insert", 1, null, 103);
    client.callProcedure("t.insert", 1, null, 104);
    client.callProcedure("t.insert", 1, null, 105);
    client.callProcedure("t.insert", 2, 1, 200);
    client.callProcedure("t.insert", 2, 2, 201);
    client.callProcedure("t.insert", 2, 3, 202);
    client.callProcedure("t.insert", 2, 4, 203);
    client.callProcedure("t.insert", 2, 5, 204);
    client.callProcedure("t.insert", 2, 4, 205);
    validateCount(client, "select a, c, count(*) over (partition by a order by b) from t order by a, c", //       postgresql, where the nulls sort after.
    new long[][] { { 1, 100, 4 }, { 1, 101, 5 }, { 1, 102, 6 }, { 1, 103, 3 }, { 1, 104, 3 }, { 1, 105, 3 }, { 2, 200, 1 }, { 2, 201, 2 }, { 2, 202, 3 }, { 2, 203, 5 }, { 2, 204, 6 }, { 2, 205, 5 } });
    validateCount(client, "select a, c, count(b) over (partition by a order by b) from t order by a, c", //       we get counts of 0.
    new long[][] { { 1, 100, 1 }, { 1, 101, 2 }, { 1, 102, 3 }, { 1, 103, 0 }, { 1, 104, 0 }, { 1, 105, 0 }, { 2, 200, 1 }, { 2, 201, 2 }, { 2, 202, 3 }, { 2, 203, 5 }, { 2, 204, 6 }, { 2, 205, 5 } });
    validateCount(client, "select a, c, count(b) over (partition by a order by b desc) from t order by a, c", //       don't count, we get counts of 0.
    new long[][] { { 1, 100, 3 }, { 1, 101, 2 }, { 1, 102, 1 }, { 1, 103, 3 }, { 1, 104, 3 }, { 1, 105, 3 }, { 2, 200, 6 }, { 2, 201, 5 }, { 2, 202, 4 }, { 2, 203, 3 }, { 2, 204, 1 }, { 2, 205, 3 } });
    // Test that COUNT(E) works if E is a timestamp.
    long baseTime = TimestampType.millisFromJDBCformat("1953-06-10 00:00:00");
    TimestampType baseTimestamp = new TimestampType(baseTime);
    client.callProcedure("T_TIMESTAMP.insert", 100, 100, baseTimestamp);
    client.callProcedure("T_TIMESTAMP.insert", 100, 100, null);
    client.callProcedure("T_TIMESTAMP.insert", 100, 100, baseTimestamp);
    validateCount(client, "select count(c) over() from T_TIMESTAMP", new long[][] { { 2 }, { 2 }, { 2 } });
    // Try some things on an empty table.
    // We expect to get no answers anywhere.
    client.callProcedure("@AdHoc", "TRUNCATE TABLE T;");
    validateCount(client, "select a, c, count(b) over (partition by a order by b desc) from t order by a, c", new long[][] {});
    validateCount(client, "select a, c, count(b) over (partition by a) from t order by a, c", new long[][] {});
    validateCount(client, "select a, c, count(b) over (order by b desc) from t order by a, c", new long[][] {});
    validateCount(client, "select a, c, count(b) over () from t order by a, c", new long[][] {});
    validateCount(client, "select a, c, count(*) over (partition by a order by b desc) from t order by a, c", new long[][] {});
    validateCount(client, "select a, c, count(*) over (partition by a) from t order by a, c", new long[][] {});
    validateCount(client, "select a, c, count(*) over (order by b desc) from t order by a, c", new long[][] {});
    validateCount(client, "select a, c, count(*) over () from t order by a, c", new long[][] {});
}
Also used : TimestampType(org.voltdb.types.TimestampType) Client(org.voltdb.client.Client)

Aggregations

TimestampType (org.voltdb.types.TimestampType)127 VoltTable (org.voltdb.VoltTable)37 BigDecimal (java.math.BigDecimal)30 Test (org.junit.Test)23 Client (org.voltdb.client.Client)19 ArrayList (java.util.ArrayList)16 IOException (java.io.IOException)11 GeographyValue (org.voltdb.types.GeographyValue)11 GeographyPointValue (org.voltdb.types.GeographyPointValue)10 Date (java.util.Date)9 File (java.io.File)7 VoltTableRow (org.voltdb.VoltTableRow)7 ProcCallException (org.voltdb.client.ProcCallException)7 ByteBuffer (java.nio.ByteBuffer)5 SimpleDateFormat (java.text.SimpleDateFormat)5 ColumnInfo (org.voltdb.VoltTable.ColumnInfo)5 ClientResponse (org.voltdb.client.ClientResponse)5 Random (java.util.Random)4 VoltType (org.voltdb.VoltType)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2