|
19 | 19 |
|
20 | 20 | package org.apache.iotdb.db.queryengine.transformation.dag.column.unary.scalar; |
21 | 21 |
|
| 22 | +import org.apache.iotdb.calc.transformation.dag.column.leaf.NullColumnTransformer; |
| 23 | +import org.apache.iotdb.calc.transformation.dag.column.unary.scalar.DateBinFunctionColumnTransformer; |
| 24 | +import org.apache.iotdb.commons.conf.CommonDescriptor; |
| 25 | + |
22 | 26 | import org.junit.Before; |
23 | 27 | import org.junit.Test; |
24 | 28 |
|
|
28 | 32 | import java.util.TimeZone; |
29 | 33 |
|
30 | 34 | import static org.apache.iotdb.calc.transformation.dag.column.unary.scalar.DateBinFunctionColumnTransformer.dateBin; |
| 35 | +import static org.apache.iotdb.calc.transformation.dag.column.unary.scalar.DateBinFunctionColumnTransformer.nextDateBin; |
| 36 | +import static org.apache.tsfile.read.common.type.TimestampType.TIMESTAMP; |
| 37 | +import static org.junit.Assert.assertArrayEquals; |
31 | 38 | import static org.junit.Assert.assertEquals; |
32 | 39 |
|
33 | 40 | public class DateBinFunctionTest { |
@@ -126,4 +133,53 @@ public void testSourceBeforeUnixEpoch() throws ParseException { |
126 | 133 | zoneId); |
127 | 134 | assertEquals(getTimestamp("1969-12-31 00:00:00.000"), result); |
128 | 135 | } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testMonthBinKeepsFractionalOrigin() throws ParseException { |
| 139 | + long origin = 500L; |
| 140 | + long oneMonthLater = getTimestamp("1970-02-01 00:00:00.500"); |
| 141 | + long justBeforeNextMonth = oneMonthLater - 1; |
| 142 | + |
| 143 | + assertEquals(origin, dateBin(origin, origin, 1, 0, zoneId)); |
| 144 | + assertEquals(origin, dateBin(justBeforeNextMonth, origin, 1, 0, zoneId)); |
| 145 | + assertEquals(oneMonthLater, dateBin(oneMonthLater, origin, 1, 0, zoneId)); |
| 146 | + |
| 147 | + long marchOrigin = getTimestamp("2024-03-15 00:00:00.500"); |
| 148 | + long aprilOrigin = getTimestamp("2024-04-15 00:00:00.500"); |
| 149 | + assertEquals(marchOrigin, dateBin(marchOrigin, marchOrigin, 1, 0, zoneId)); |
| 150 | + assertEquals(aprilOrigin, dateBin(aprilOrigin, marchOrigin, 1, 0, zoneId)); |
| 151 | + |
| 152 | + long exactSecond = getTimestamp("2024-03-15 00:00:00.000"); |
| 153 | + long nextExactMonth = getTimestamp("2024-04-15 00:00:00.000"); |
| 154 | + assertEquals(exactSecond, dateBin(exactSecond, exactSecond, 1, 0, zoneId)); |
| 155 | + assertEquals(nextExactMonth, dateBin(nextExactMonth, exactSecond, 1, 0, zoneId)); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testMonthBinStartEndAndGapFillKeepFractionalMillis() throws ParseException { |
| 160 | + long origin = 500L; |
| 161 | + DateBinFunctionColumnTransformer transformer = |
| 162 | + new DateBinFunctionColumnTransformer( |
| 163 | + TIMESTAMP, 1, 0, new NullColumnTransformer(), origin, zoneId); |
| 164 | + |
| 165 | + long oneMonthLater = getTimestamp("1970-02-01 00:00:00.500"); |
| 166 | + assertArrayEquals(new long[] {origin, oneMonthLater}, transformer.dateBinStartEnd(origin)); |
| 167 | + assertEquals(oneMonthLater, nextDateBin(1, zoneId, origin)); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testMonthBinKeepsSubMillisecondOriginUnderUsAndNs() { |
| 172 | + String originalPrecision = CommonDescriptor.getInstance().getConfig().getTimestampPrecision(); |
| 173 | + try { |
| 174 | + // 1.5 ms after the epoch, expressed in microseconds and nanoseconds. |
| 175 | + CommonDescriptor.getInstance().getConfig().setTimestampPrecision("us"); |
| 176 | + assertEquals(1500L, dateBin(1500L, 1500L, 1, 0, zoneId)); |
| 177 | + assertEquals(1500L, nextDateBin(1, zoneId, 1500L) - 2678400000000L); |
| 178 | + |
| 179 | + CommonDescriptor.getInstance().getConfig().setTimestampPrecision("ns"); |
| 180 | + assertEquals(1_500_000L, dateBin(1_500_000L, 1_500_000L, 1, 0, zoneId)); |
| 181 | + } finally { |
| 182 | + CommonDescriptor.getInstance().getConfig().setTimestampPrecision(originalPrecision); |
| 183 | + } |
| 184 | + } |
129 | 185 | } |
0 commit comments