Search in sources :

Example 6 with GeographyPointValue

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

the class RegressionSuite method assertApproximatelyEquals.

/**
     * Assert that two geography values are approximately equal.  By approximately
     * equal we mean that the vertices of the expected and actual values differ
     * by at most epsilon.  If epsilon is not positive this means the values must
     * be exactly equal.
     *
     * @param msg
     * @param expected
     * @param actual
     * @param epsilon
     */
public static void assertApproximatelyEquals(String msg, GeographyValue expected, GeographyValue actual, double epsilon) {
    if (expected == actual) {
        return;
    }
    // caller checks for null in the expected value
    assert (expected != null);
    if (actual == null) {
        fail(msg + " found null value when non-null expected");
    }
    List<List<GeographyPointValue>> expectedLoops = expected.getRings();
    List<List<GeographyPointValue>> actualLoops = actual.getRings();
    assertEquals(msg + "wrong number of loops, expected " + expectedLoops.size() + ", " + "got " + actualLoops.size(), expectedLoops.size(), actualLoops.size());
    int loopCtr = 0;
    Iterator<List<GeographyPointValue>> expectedLoopIt = expectedLoops.iterator();
    for (List<GeographyPointValue> actualLoop : actualLoops) {
        List<GeographyPointValue> expectedLoop = expectedLoopIt.next();
        assertEquals(msg + loopCtr + "the loop should have " + expectedLoop.size() + " vertices, but has " + actualLoop.size(), expectedLoop.size(), actualLoop.size());
        int vertexCtr = 0;
        Iterator<GeographyPointValue> expectedVertexIt = expectedLoop.iterator();
        for (GeographyPointValue actualPt : actualLoop) {
            GeographyPointValue expectedPt = expectedVertexIt.next();
            String prefix = msg + "at loop " + loopCtr + ", vertex " + vertexCtr;
            assertApproximatelyEquals(prefix, expectedPt, actualPt, epsilon);
            ++vertexCtr;
        }
        ++loopCtr;
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) GeographyPointValue(org.voltdb.types.GeographyPointValue)

Example 7 with GeographyPointValue

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

the class PolygonFactory method CreateRegularConvex.

/**
     * Create a regular convex polygon, with an optional hole.
     *
     * Note that the resulting polygon will be symmetric around any line
     * through the center and a vertex.  Consequently, the centroid of such
     * a polygon must be the center of the polygon.
     *
     * @param center The center of the polygon.
     * @param firstVertex The coordinates of the first vertex.
     * @param numVertices The number of vertices.
     * @param sizeOfHole If this is positive, we also create a hole whose vertices are
     *                   at the same angle from the polygon's center, but whose distance
     *                   is scaled by sizeOfHole.  This value must be in the range [0,1).
     * @return
     */
public static GeographyValue CreateRegularConvex(GeographyPointValue center, GeographyPointValue firstVertex, int numVertices, double sizeOfHole) {
    assert (0 <= sizeOfHole && sizeOfHole < 1.0);
    double phi = 360.0 / numVertices;
    GeographyPointValue holeFirstVertex = null;
    if (sizeOfHole > 0) {
        holeFirstVertex = firstVertex.scale(center, sizeOfHole);
    }
    List<GeographyPointValue> oneLoop = new ArrayList<GeographyPointValue>();
    List<GeographyPointValue> hole = (sizeOfHole < 0 ? null : new ArrayList<GeographyPointValue>());
    // as points.
    for (int idx = 0; idx < numVertices; idx += 1) {
        oneLoop.add(firstVertex.rotate(idx * phi, center));
        if (sizeOfHole > 0) {
            hole.add(holeFirstVertex.rotate(-(idx * phi), center));
        }
    }
    // Add the closing vertices.
    oneLoop.add(firstVertex);
    if (sizeOfHole > 0) {
        hole.add(holeFirstVertex);
    }
    List<List<GeographyPointValue>> loops = new ArrayList<List<GeographyPointValue>>();
    loops.add(oneLoop);
    if (sizeOfHole > 0) {
        loops.add(hole);
    }
    return new GeographyValue(loops);
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) GeographyPointValue(org.voltdb.types.GeographyPointValue)

Example 8 with GeographyPointValue

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

the class RegressionSuite method assertApproximateContentOfRow.

private static void assertApproximateContentOfRow(int row, Object[] expectedRow, VoltTable actualRow, double epsilon) {
    assertEquals("Actual row has wrong number of columns", expectedRow.length, actualRow.getColumnCount());
    for (int i = 0; i < expectedRow.length; ++i) {
        String msg = "Row " + row + ", col " + i + ": ";
        Object expectedObj = expectedRow[i];
        if (expectedObj == null) {
            VoltType vt = actualRow.getColumnType(i);
            actualRow.get(i, vt);
            assertTrue(msg, actualRow.wasNull());
        } else if (expectedObj instanceof GeographyPointValue) {
            assertApproximatelyEquals(msg, (GeographyPointValue) expectedObj, actualRow.getGeographyPointValue(i), epsilon);
        } else if (expectedObj instanceof GeographyValue) {
            assertApproximatelyEquals(msg, (GeographyValue) expectedObj, actualRow.getGeographyValue(i), epsilon);
        } else if (expectedObj instanceof Long) {
            long val = ((Long) expectedObj).longValue();
            assertEquals(msg, val, actualRow.getLong(i));
        } else if (expectedObj instanceof Integer) {
            long val = ((Integer) expectedObj).longValue();
            assertEquals(msg, val, actualRow.getLong(i));
        } else if (expectedObj instanceof Double) {
            double expectedValue = (Double) expectedObj;
            double actualValue = actualRow.getDouble(i);
            // for null values, convert value into double min
            if (actualRow.wasNull()) {
                actualValue = Double.MIN_VALUE;
            }
            if (epsilon <= 0) {
                String fullMsg = msg + String.format("Expected value %f != actual value %f", expectedValue, actualValue);
                assertEquals(fullMsg, expectedValue, actualValue);
            } else {
                String fullMsg = msg + String.format("abs(Expected Value - Actual Value) = %e >= %e", Math.abs(expectedValue - actualValue), epsilon);
                assertTrue(fullMsg, Math.abs(expectedValue - actualValue) < epsilon);
            }
        } else if (expectedObj instanceof BigDecimal) {
            BigDecimal exp = (BigDecimal) expectedObj;
            BigDecimal got = actualRow.getDecimalAsBigDecimal(i);
            // Either both are null or neither are null.
            assertEquals(exp == null, got == null);
            assertEquals(msg, exp.doubleValue(), got.doubleValue(), epsilon);
        } else if (expectedObj instanceof String) {
            String val = (String) expectedObj;
            assertEquals(msg, val, actualRow.getString(i));
        } else if (expectedObj instanceof TimestampType) {
            TimestampType val = (TimestampType) expectedObj;
            assertEquals(msg, val, actualRow.getTimestampAsTimestamp(i));
        } else {
            fail("Unexpected type in expected row: " + expectedObj.getClass().getSimpleName());
        }
    }
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) VoltType(org.voltdb.VoltType) TimestampType(org.voltdb.types.TimestampType) GeographyPointValue(org.voltdb.types.GeographyPointValue) BigDecimal(java.math.BigDecimal)

Example 9 with GeographyPointValue

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

the class TestGeographyPointValue method fillTableWithFunnyPoints.

private void fillTableWithFunnyPoints(Client client) throws Exception {
    // 1/2 of epsilon
    final double lessThanEps = 1e-13;
    // two times epsilon
    final double moreThanEps = 2e-12;
    int i = 0;
    client.callProcedure("t.Insert", i++, "point", new GeographyPointValue(10.333, 20.666));
    client.callProcedure("t.Insert", i++, "closePoint", new GeographyPointValue(10.333 + lessThanEps, 20.666 - lessThanEps));
    client.callProcedure("t.Insert", i++, "farPoint", new GeographyPointValue(0.0, 10.0));
    client.callProcedure("t.Insert", i++, "northPole1", new GeographyPointValue(50.0, 90.0));
    client.callProcedure("t.Insert", i++, "northPole2", new GeographyPointValue(-70.0, 90.0));
    client.callProcedure("t.Insert", i++, "northPole3", new GeographyPointValue(10.0, 90.0 - lessThanEps));
    client.callProcedure("t.Insert", i++, "northPole4", new GeographyPointValue(180.0, 90.0 - lessThanEps));
    client.callProcedure("t.Insert", i++, "northPole5", new GeographyPointValue(-180.0, 90.0 - lessThanEps));
    client.callProcedure("t.Insert", i++, "notNorthPole", new GeographyPointValue(10.0, 90.0 - moreThanEps));
    client.callProcedure("t.Insert", i++, "southPole1", new GeographyPointValue(50.0, -90.0));
    client.callProcedure("t.Insert", i++, "southPole2", new GeographyPointValue(-70.0, -90.0));
    client.callProcedure("t.Insert", i++, "southPole3", new GeographyPointValue(10.0, -90.0 + lessThanEps));
    client.callProcedure("t.Insert", i++, "southPole4", new GeographyPointValue(180.0, -90.0 + lessThanEps));
    client.callProcedure("t.Insert", i++, "southPole5", new GeographyPointValue(-180.0, -90.0 + lessThanEps));
    client.callProcedure("t.Insert", i++, "notSouthPole", new GeographyPointValue(10.0, -90.0 + moreThanEps));
    client.callProcedure("t.Insert", i++, "onAntimeridianNeg1", new GeographyPointValue(-180.0, 37.0));
    client.callProcedure("t.Insert", i++, "onAntimeridianNeg2", new GeographyPointValue(-180.0 + lessThanEps, 37.0));
    client.callProcedure("t.Insert", i++, "onAntimeridianPos1", new GeographyPointValue(180.0, 37.0));
    client.callProcedure("t.Insert", i++, "onAntimeridianPos2", new GeographyPointValue(180.0 - lessThanEps, 37.0));
    client.callProcedure("t.Insert", i++, "notOnIDLNeg", new GeographyPointValue(-180.0 + moreThanEps, 37.0));
    client.callProcedure("t.Insert", i++, "notOnIDLPos", new GeographyPointValue(180.0 - moreThanEps, 37.0));
}
Also used : GeographyPointValue(org.voltdb.types.GeographyPointValue)

Example 10 with GeographyPointValue

use of org.voltdb.types.GeographyPointValue 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)

Aggregations

GeographyPointValue (org.voltdb.types.GeographyPointValue)38 GeographyValue (org.voltdb.types.GeographyValue)23 VoltTable (org.voltdb.VoltTable)11 BigDecimal (java.math.BigDecimal)10 TimestampType (org.voltdb.types.TimestampType)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Client (org.voltdb.client.Client)7 IOException (java.io.IOException)5 JSONString (org.json_voltpatches.JSONString)3 VoltType (org.voltdb.VoltType)3 ByteBuffer (java.nio.ByteBuffer)2 ColumnInfo (org.voltdb.VoltTable.ColumnInfo)2 NullCallback (org.voltdb.client.NullCallback)2 File (java.io.File)1 BigInteger (java.math.BigInteger)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 SimpleDateFormat (java.text.SimpleDateFormat)1