Elffuss BraidHub

kikocis/java-driver public

DateCodec does not format values correctly (JAVA-816) - TimeCodec does not format values correctly (JAVA-817).
Changesetda0c3d4bdee0a5ffeab0cea71394ffd88780ff919d9b99af28434a96c2ac2c98
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-24 16:12 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-07-20 19:50 +02:00
Parentsa24f48a9b779

Browse the files at this changeset

4 files changed

modified changelog/README.md +3 −1
@@ -3,7 +3,9 @@
33 ### 2.2.0-rc2 (in progress)
44
55 - [improvement] Rename DateWithoutTime to LocalDate (JAVA-810)
6-- [bug] TypeCodec.getDataTypeFor() does not handle LocalDate instances. (JAVA-818)
6+- [bug] DateCodec does not format values correctly (JAVA-816)
7+- [bug] TimeCodec does not format values correctly (JAVA-817)
8+- [bug] TypeCodec.getDataTypeFor() does not handle LocalDate instances (JAVA-818)
79
810 Merged from 2.1 branch:
911
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +47 −20
@@ -908,6 +908,11 @@
908908
909909 @Override
910910 public Date parse(String value) {
911+ // strip enclosing single quotes, if any
912+ // enclosing quotes are optional for long literals, mandatory for date patterns
913+ if (value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\'')
914+ value = value.substring(1, value.length() - 1);
915+
911916 if (IS_LONG_PATTERN.matcher(value).matches()) {
912917 try {
913918 return new Date(Long.parseLong(value));
@@ -952,6 +957,12 @@
952957
953958 @Override
954959 public LocalDate parse(String value) {
960+
961+ // strip enclosing single quotes, if any
962+ // single quotes are optional for long literals, mandatory for date patterns
963+ if (value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\'')
964+ value = value.substring(1, value.length() - 1);
965+
955966 if (IS_LONG_PATTERN.matcher(value).matches()) {
956967 // In CQL, numeric DATE literals are longs between 0 and 2^32 - 1, with the epoch in the middle,
957968 // so parse it as a long and re-center at 0
@@ -985,7 +996,7 @@
985996
986997 @Override
987998 public String format(LocalDate value) {
988- return value.toString();
999+ return "'" + value.toString() + "'";
9891000 }
9901001
9911002 @Override
@@ -1010,7 +1021,7 @@
10101021 }
10111022 }
10121023
1013- static class TimeCodec extends TypeCodec<Long> {
1024+ static class TimeCodec extends LongCodec {
10141025
10151026 private static final Pattern IS_LONG_PATTERN = Pattern.compile("^-?\\d+$");
10161027
@@ -1082,6 +1093,11 @@
10821093
10831094 @Override
10841095 public Long parse(String value) {
1096+ // enclosing single quotes required, even for long literals
1097+ if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'')
1098+ throw new InvalidTypeException("time values must be enclosed by single quotes");
1099+ value = value.substring(1, value.length() - 1);
1100+
10851101 if (IS_LONG_PATTERN.matcher(value).matches()) {
10861102 try {
10871103 return Long.parseLong(value);
@@ -1099,26 +1115,37 @@
10991115
11001116 @Override
11011117 public String format(Long value) {
1102- return value.toString();
1103- }
1104-
1105- @Override
1106- public ByteBuffer serialize(Long value) {
1107- return LongCodec.instance.serializeNoBoxing(value);
1108- }
1109-
1110- @Override
1111- public Long deserialize(ByteBuffer bytes) {
1112- return LongCodec.instance.deserializeNoBoxing(bytes);
1113- }
1114-
1115- public ByteBuffer serializeNoBoxing(long value) {
1116- return LongCodec.instance.serializeNoBoxing(value);
1118+ if (value == null)
1119+ return "null";
1120+ int nano = (int)(value % 1000000000);
1121+ value -= nano;
1122+ value /= 1000000000;
1123+ int seconds = (int)(value % 60);
1124+ value -= seconds;
1125+ value /= 60;
1126+ int minutes = (int)(value % 60);
1127+ value -= minutes;
1128+ value /= 60;
1129+ int hours = (int)(value % 24);
1130+ value -= hours;
1131+ value /= 24;
1132+ assert(value == 0);
1133+ StringBuilder sb = new StringBuilder("'");
1134+ leftPadZeros(hours, 2, sb);
1135+ sb.append(":");
1136+ leftPadZeros(minutes, 2, sb);
1137+ sb.append(":");
1138+ leftPadZeros(seconds, 2, sb);
1139+ sb.append(".");
1140+ leftPadZeros(nano, 9, sb);
1141+ sb.append("'");
1142+ return sb.toString();
1143+ }
1144+
1145+ private static void leftPadZeros(int value, int digits, StringBuilder sb) {
1146+ sb.append(String.format("%0" + digits + "d", value));
11171147 }
11181148
1119- public long deserializeNoBoxing(ByteBuffer bytes) {
1120- return LongCodec.instance.deserializeNoBoxing(bytes);
1121- }
11221149 }
11231150
11241151 static class UUIDCodec extends TypeCodec<UUID> {
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeIntegrationTest.java +12 −1
@@ -15,6 +15,7 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.nio.ByteBuffer;
1819 import java.util.*;
1920 import java.util.concurrent.atomic.AtomicInteger;
2021
@@ -89,7 +90,9 @@
8990 session.execute(table.insertStatement.replace("?", table.testColumnType.format(table.sampleValue)));
9091 break;
9192 case SIMPLE_WITH_PARAM:
92- session.execute(table.insertStatement, table.sampleValue);
93+ SimpleStatement statement = new SimpleStatement(table.insertStatement, table.sampleValue);
94+ checkGetValuesReturnsSerializedValue(protocolVersion, statement, table);
95+ session.execute(statement);
9396 break;
9497 case PREPARED:
9598 PreparedStatement ps = session.prepare(table.insertStatement);
@@ -123,6 +126,14 @@
123126 assertThat(bs.getObject("v")).isEqualTo(table.sampleValue);
124127 }
125128
129+ public void checkGetValuesReturnsSerializedValue(ProtocolVersion protocolVersion, SimpleStatement statement, TestTable table) {
130+ ByteBuffer[] values = statement.getValues(protocolVersion);
131+ assertThat(values.length).isEqualTo(1);
132+ assertThat(values[0])
133+ .as("Value not serialized as expected for " + table.sampleValue)
134+ .isEqualTo(DataType.serializeValue(table.sampleValue, protocolVersion));
135+ }
136+
126137 /**
127138 * Abstracts information about a table (corresponding to a given column type).
128139 */
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeTest.java +78 −66
@@ -23,7 +23,6 @@
2323
2424 import com.google.common.collect.Lists;
2525 import com.google.common.reflect.TypeToken;
26-
2726 import org.testng.annotations.Test;
2827
2928 import static org.testng.Assert.assertEquals;
@@ -43,90 +42,103 @@
4342 return t.getName() == DataType.Name.COUNTER;
4443 }
4544
46- private static String[] getCQLStringTestData(DataType dt) {
47- switch (dt.getName()) {
48- case ASCII:
49- case TEXT:
50- case VARCHAR:
51- return new String[]{ "'foo'", "'fo''o'" };
52- case BIGINT:
53- case TIMESTAMP:
54- return new String[]{ "42", "91294377723", "-133" };
55- case DATE:
56- return new String[]{ "2014-01-01", "1970-01-01" };
57- case TIME:
58- return new String[]{ "54012123450000", "0" };
59- case BLOB:
60- return new String[]{ "0x2450", "0x" };
61- case BOOLEAN:
62- return new String[]{ "true", "false" };
63- case DECIMAL:
64- return new String[]{ "1.23E+8" };
65- case DOUBLE:
66- return new String[]{ "2.39324324", "-12.0" };
67- case FLOAT:
68- return new String[]{ "2.39", "-12.0" };
69- case INET:
70- return new String[]{ "'128.2.12.3'" };
71- case TINYINT:
72- return new String[]{ "-4", "44" };
73- case SMALLINT:
74- return new String[]{ "-3", "43" };
75- case INT:
76- return new String[]{ "-2", "42" };
77- case TIMEUUID:
78- return new String[]{ "fe2b4360-28c6-11e2-81c1-0800200c9a66" };
79- case UUID:
80- return new String[]{ "fe2b4360-28c6-11e2-81c1-0800200c9a66", "067e6162-3b6f-4ae2-a171-2470b63dff00" };
81- case VARINT:
82- return new String[]{ "12387290982347987032483422342432" };
83- default:
84- throw new RuntimeException("Missing handling of " + dt);
45+ /** A test value for a primitive data type */
46+ static class TestValue {
47+ /** The value as a Java object */
48+ final Object javaObject;
49+ /** A CQL string that should parse to the value */
50+ final String cqlInputString;
51+ /** How the value should be formatted in CQL */
52+ final String cqlOutputString;
53+
54+ TestValue(Object javaObject, String cqlInputString, String cqlOutputString) {
55+ this.javaObject = javaObject;
56+ this.cqlInputString = cqlInputString;
57+ this.cqlOutputString = cqlOutputString;
8558 }
8659 }
8760
88- private static Object[] getTestData(DataType dt) {
61+ private static TestValue[] primitiveTestValues(DataType dt) {
8962 switch (dt.getName()) {
9063 case ASCII:
9164 case TEXT:
9265 case VARCHAR:
93- return new Object[]{ "foo", "fo'o" };
66+ return new TestValue[]{
67+ new TestValue("foo", "'foo'", "'foo'"),
68+ new TestValue("fo'o", "'fo''o'", "'fo''o'") };
9469 case BIGINT:
95- return new Object[]{ 42L, 91294377723L, -133L };
70+ return new TestValue[]{
71+ new TestValue(42L, "42", "42"),
72+ new TestValue(91294377723L, "91294377723", "91294377723"),
73+ new TestValue(-133L, "-133", "-133") };
9674 case TIMESTAMP:
97- return new Object[]{ new Date(42L), new Date(91294377723L), new Date(-133L) };
75+ // input: single quotes are optional for long literals, mandatory for date patterns
76+ return new TestValue[]{
77+ new TestValue(new Date(42L), "42", "42"),
78+ new TestValue(new Date(91294377723L), "91294377723", "91294377723"),
79+ new TestValue(new Date(-133L), "-133", "-133"),
80+ new TestValue(new Date(784041330999L), "'1994-11-05T14:15:30.999+0100'", "784041330999") };
9881 case DATE:
99- return new Object[]{ LocalDate.fromDaysSinceEpoch(16071) /* 2014-01-01 */, LocalDate.fromDaysSinceEpoch(0) /* 1970-01-01 */ };
82+ // input: single quotes are optional for long literals, mandatory for date patterns
83+ return new TestValue[]{
84+ new TestValue(LocalDate.fromDaysSinceEpoch(16071), "'2014-01-01'", "'2014-01-01'"),
85+ new TestValue(LocalDate.fromDaysSinceEpoch(0), "'1970-01-01'", "'1970-01-01'"),
86+ new TestValue(LocalDate.fromDaysSinceEpoch((int)(2147483648L - (1L << 31))), "'2147483648'", "'1970-01-01'"),
87+ new TestValue(LocalDate.fromDaysSinceEpoch((int)(0 - (1L << 31))), "0", "'-5877641-06-23'") };
10088 case TIME:
101- return new Object[]{ 54012123450000L /* 15:00:12.123450000 */, 0L };
89+ // input: all literals must by enclosed in single quotes
90+ return new TestValue[]{
91+ new TestValue(54012123450000L, "'54012123450000'", "'15:00:12.123450000'"),
92+ new TestValue(0L, "'0'", "'00:00:00.000000000'"),
93+ new TestValue(54012012345000L, "'15:00:12.012345000'", "'15:00:12.012345000'") };
10294 case BLOB:
103- return new Object[]{ Bytes.fromHexString("0x2450"), ByteBuffer.allocate(0) };
95+ return new TestValue[]{
96+ new TestValue(Bytes.fromHexString("0x2450"), "0x2450", "0x2450"),
97+ new TestValue(ByteBuffer.allocate(0), "0x", "0x") };
10498 case BOOLEAN:
105- return new Object[]{ true, false };
99+ return new TestValue[]{
100+ new TestValue(true, "true", "true"),
101+ new TestValue(false, "false", "false") };
106102 case DECIMAL:
107- return new Object[]{ new BigDecimal("1.23E+8") };
103+ return new TestValue[]{
104+ new TestValue(new BigDecimal("1.23E+8"), "1.23E+8", "1.23E+8") };
108105 case DOUBLE:
109- return new Object[]{ 2.39324324, -12. };
106+ return new TestValue[]{
107+ new TestValue(2.39324324, "2.39324324", "2.39324324"),
108+ new TestValue(-12., "-12.0", "-12.0") };
110109 case FLOAT:
111- return new Object[]{ 2.39f, -12.f };
110+ return new TestValue[]{
111+ new TestValue(2.39f, "2.39", "2.39"),
112+ new TestValue(-12.f, "-12.0", "-12.0") };
112113 case INET:
113114 try {
114- return new Object[]{ InetAddress.getByName("128.2.12.3") };
115+ return new TestValue[]{
116+ new TestValue(InetAddress.getByName("128.2.12.3"), "'128.2.12.3'", "'128.2.12.3'") };
115117 } catch (java.net.UnknownHostException e) {
116118 throw new RuntimeException();
117119 }
118120 case TINYINT:
119- return new Object[]{ (byte)-4, (byte)44 };
121+ return new TestValue[]{
122+ new TestValue((byte)-4, "-4", "-4"),
123+ new TestValue((byte)44, "44", "44") };
120124 case SMALLINT:
121- return new Object[]{ (short)-3, (short)43 };
125+ return new TestValue[]{
126+ new TestValue((short)-3, "-3", "-3"),
127+ new TestValue((short)43, "43", "43") };
122128 case INT:
123- return new Object[]{ -2, 42 };
129+ return new TestValue[]{
130+ new TestValue(-2, "-2", "-2"),
131+ new TestValue(42, "42", "42") };
124132 case TIMEUUID:
125- return new Object[]{ UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66") };
133+ return new TestValue[]{
134+ new TestValue(UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"), "fe2b4360-28c6-11e2-81c1-0800200c9a66", "fe2b4360-28c6-11e2-81c1-0800200c9a66") };
126135 case UUID:
127- return new Object[]{ UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"), UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00") };
136+ return new TestValue[]{
137+ new TestValue(UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"), "fe2b4360-28c6-11e2-81c1-0800200c9a66", "fe2b4360-28c6-11e2-81c1-0800200c9a66"),
138+ new TestValue(UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00"), "067e6162-3b6f-4ae2-a171-2470b63dff00", "067e6162-3b6f-4ae2-a171-2470b63dff00") };
128139 case VARINT:
129- return new Object[]{ new BigInteger("12387290982347987032483422342432") };
140+ return new TestValue[]{
141+ new TestValue(new BigInteger("12387290982347987032483422342432"), "12387290982347987032483422342432", "12387290982347987032483422342432") };
130142 default:
131143 throw new RuntimeException("Missing handling of " + dt);
132144 }
@@ -138,10 +150,10 @@
138150 if (exclude(dt))
139151 continue;
140152
141- String[] s = getCQLStringTestData(dt);
142- Object[] o = getTestData(dt);
143- for (int i = 0; i < s.length; i++)
144- assertEquals(dt.parse(s[i]), o[i], String.format("For input %d of %s, ", i, dt, s[i], o[i]));
153+ for (TestValue value : primitiveTestValues(dt))
154+ assertThat(dt.parse(value.cqlInputString))
155+ .as("Parsing input %s to a %s", value.cqlInputString, dt)
156+ .isEqualTo(value.javaObject);
145157 }
146158 }
147159
@@ -151,10 +163,10 @@
151163 if (exclude(dt))
152164 continue;
153165
154- String[] s = getCQLStringTestData(dt);
155- Object[] o = getTestData(dt);
156- for (int i = 0; i < s.length; i++)
157- assertEquals(dt.format(o[i]), s[i], String.format("For input %d of %s, ", i, dt, o[i], s[i]));
166+ for (TestValue value : primitiveTestValues(dt))
167+ assertThat(dt.format(value.javaObject))
168+ .as("Formatting a %s expecting %s", dt, value.cqlOutputString)
169+ .isEqualTo(value.cqlOutputString);
158170 }
159171 }
160172