Skip to content

Commit

Permalink
Fixing bug with toObject methods on DATE, TIME and TIMESTAMP PData ty…
Browse files Browse the repository at this point in the history
…pes. Adding test case for the same
  • Loading branch information
prashantkommireddi committed Apr 10, 2013
1 parent 5f23fbb commit 6fa8df9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/salesforce/phoenix/schema/PDataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,9 @@ public Object toObject(Object object, PDataType actualType) {
}
switch (actualType) {
case DATE:
case TIME:
return new Timestamp(((Date)object).getTime());
case TIME:
return new Timestamp(((Time)object).getTime());
case TIMESTAMP:
return object;
default:
Expand All @@ -805,6 +806,11 @@ public Object toObject(byte[] b, int o, int l, PDataType actualType) {
throw new ConstraintViolationException(actualType + " cannot be coerced to " + this);
}
}

@Override
public boolean isCoercibleTo(PDataType targetType) {
return this == targetType || targetType == DATE || targetType == TIME || targetType == BINARY;
}

@Override
public boolean isFixedWidth() {
Expand Down Expand Up @@ -875,8 +881,9 @@ public Object toObject(Object object, PDataType actualType) {
}
switch (actualType) {
case DATE:
case TIMESTAMP:
return new Time(((Date)object).getTime());
case TIMESTAMP:
return new Time(((Timestamp)object).getTime());
case TIME:
return object;
default:
Expand Down Expand Up @@ -946,8 +953,9 @@ public Object toObject(Object object, PDataType actualType) {
}
switch (actualType) {
case TIME:
return new Date(((Time)object).getTime());
case TIMESTAMP:
return new Time(((Date)object).getTime());
return new Date(((Timestamp)object).getTime());
case DATE:
return object;
default:
Expand Down
45 changes: 43 additions & 2 deletions src/test/java/com/salesforce/phoenix/schema/PDataTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@
******************************************************************************/
package com.salesforce.phoenix.schema;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
Expand Down Expand Up @@ -327,6 +337,14 @@ public void testValueCoersion() throws Exception {
assertTrue(PDataType.UNSIGNED_LONG.isCoercibleTo(PDataType.INTEGER, 0L));
assertTrue(PDataType.UNSIGNED_LONG.isCoercibleTo(PDataType.LONG));
assertFalse(PDataType.UNSIGNED_LONG.isCoercibleTo(PDataType.UNSIGNED_INT));

// Testing coercing Date types
assertTrue(PDataType.DATE.isCoercibleTo(PDataType.TIMESTAMP));
assertTrue(PDataType.DATE.isCoercibleTo(PDataType.TIME));
assertTrue(PDataType.TIMESTAMP.isCoercibleTo(PDataType.DATE));
assertTrue(PDataType.TIMESTAMP.isCoercibleTo(PDataType.TIME));
assertTrue(PDataType.TIME.isCoercibleTo(PDataType.TIMESTAMP));
assertTrue(PDataType.TIME.isCoercibleTo(PDataType.DATE));
}

@Test
Expand Down Expand Up @@ -373,6 +391,29 @@ public void testGetDeicmalPrecisionAndScaleFromRawBytes() throws Exception {
testReadDecimalPrecisionAndScaleFromRawBytes(bds[i].negate());
}
}

@Test
public void testDateConversions() {
long now = System.currentTimeMillis();
Date date = new Date(now);
Time t = new Time(now);
Timestamp ts = new Timestamp(now);

Object o = PDataType.DATE.toObject(ts, PDataType.TIMESTAMP);
assertEquals(o.getClass(), java.sql.Date.class);
o = PDataType.DATE.toObject(t, PDataType.TIME);
assertEquals(o.getClass(), java.sql.Date.class);

o = PDataType.TIME.toObject(date, PDataType.DATE);
assertEquals(o.getClass(), java.sql.Time.class);
o = PDataType.TIME.toObject(ts, PDataType.TIMESTAMP);
assertEquals(o.getClass(), java.sql.Time.class);

o = PDataType.TIMESTAMP.toObject(date, PDataType.DATE);
assertEquals(o.getClass(), java.sql.Timestamp.class);
o = PDataType.TIMESTAMP.toObject(t, PDataType.TIME);
assertEquals(o.getClass(), java.sql.Timestamp.class);
}

private void testReadDecimalPrecisionAndScaleFromRawBytes(BigDecimal bd) {
byte[] b = PDataType.DECIMAL.toBytes(bd);
Expand Down

0 comments on commit 6fa8df9

Please sign in to comment.