Skip to content

Apache DataFusion SQL Query Engine

ARROW-835: [Format][C++][Java] Create a new Duration type

Micah Kornfielde7a52f90638c
diff --git a/format/Schema.fbs b/format/Schema.fbs
index 3f26e95ce7..36127925ef 100644
--- a/format/Schema.fbs
+++ b/format/Schema.fbs
@@ -30,7 +30,7 @@ enum MetadataVersion:short {
V3,
/// >= 0.8.0
- V4
+ V4,
}
/// These are stored in the flatbuffer in the Type union below
@@ -183,10 +183,35 @@ table Timestamp {
}
enum IntervalUnit: short { YEAR_MONTH, DAY_TIME}
+// A "calendar" interval which models types that don't necessarily
+// have a precise duration without the context of a base timestamp (e.g.
+// days can differ in length during day light savings time transitions).
+// YEAR_MONTH - Indicates the number of elapsed whole months, stored as
+// 4-byte integers.
+// DAY_TIME - Indicates the number of elapsed days and milliseconds,
+// stored as 2 contiguous 32-bit integers (8-bytes in total). Support
+// of this IntervalUnit is not required for full arrow compatibility.
table Interval {
unit: IntervalUnit;
}
+// An absolute length of time unrelated to any calendar artifacts.
+//
+// For the purposes of Arrow Implementations, adding this value to a Timestamp
+// ("t1") naively (i.e. simply summing the two number) is acceptable even
+// though in some cases the resulting Timestamp (t2) would not account for
+// leap-seconds during the elapsed time between "t1" and "t2". Similarly,
+// representing the difference between two Unix timestamp is acceptable, but
+// would yield a value that is possibly a few seconds off from the true elapsed
+// time.
+//
+// The resolution defaults to millisecond, but can be any of the other
+// supported TimeUnit values as with Timestamp and Time types. This type is
+// always represented as an 8-byte integer.
+table Duration {
+ unit: TimeUnit = MILLISECOND;
+}
+
/// ----------------------------------------------------------------------
/// Top-level Type value, enabling extensible type-specific metadata. We can
/// add new logical types to Type without breaking backwards compatibility
@@ -208,7 +233,8 @@ union Type {
Union,
FixedSizeBinary,
FixedSizeList,
- Map
+ Map,
+ Duration,
}
/// ----------------------------------------------------------------------
diff --git a/integration/integration_test.py b/integration/integration_test.py
index b184cd2cdb..4c3a354d31 100644
--- a/integration/integration_test.py
+++ b/integration/integration_test.py
@@ -328,6 +328,68 @@ class TimestampType(IntegerType):
return OrderedDict(fields)
+class DurationIntervalType(IntegerType):
+ def __init__(self, name, unit='s', nullable=True):
+ min_val, max_val = np.iinfo('int64').min, np.iinfo('int64').max,
+ super(DurationIntervalType, self).__init__(
+ name, True, 64, nullable=nullable,
+ min_value=min_val,
+ max_value=max_val)
+ self.unit = unit
+
+ def _get_type(self):
+ fields = [
+ ('name', 'duration'),
+ ('unit', TIMEUNIT_NAMES[self.unit])
+ ]
+
+ return OrderedDict(fields)
+
+
+class YearMonthIntervalType(IntegerType):
+ def __init__(self, name, nullable=True):
+ min_val, max_val = [-10000*12, 10000*12] # +/- 10000 years.
+ super(YearMonthIntervalType, self).__init__(
+ name, True, 32, nullable=nullable,
+ min_value=min_val,
+ max_value=max_val)
+
+ def _get_type(self):
+ fields = [
+ ('name', 'interval'),
+ ('unit', 'YEAR_MONTH'),
+ ]
+
+ return OrderedDict(fields)
+
+
+class DayTimeIntervalType(PrimitiveType):
+ def __init__(self, name, nullable=True):
+ super(DayTimeIntervalType, self).__init__(name, nullable=True)
+
+ @property
+ def numpy_type(self):
+ return object
+
+ def _get_type(self):
+
+ return OrderedDict([
+ ('name', 'interval'),
+ ('unit', 'DAY_TIME'),
+ ])
+
+ def generate_column(self, size, name=None):
+ min_day_value, max_day_value = -10000*366, 10000*366
+ values = [{'days': random.randint(min_day_value, max_day_value),
+ 'milliseconds': random.randint(-86400000, +86400000)}
+ for _ in range(size)]
+
+ is_valid = self._make_is_valid(size)
+ if name is None:
+ name = self.name
+ return PrimitiveColumn(name, size, is_valid, values)
+
+
class FloatingPointType(PrimitiveType):
def __init__(self, name, bit_width, nullable=True):
@@ -875,13 +937,27 @@ def generate_datetime_case():
TimestampType('f11', 's', tz='UTC'),
TimestampType('f12', 'ms', tz='US/Eastern'),
TimestampType('f13', 'us', tz='Europe/Paris'),
- TimestampType('f14', 'ns', tz='US/Pacific')
+ TimestampType('f14', 'ns', tz='US/Pacific'),
]
batch_sizes = [7, 10]
return _generate_file("datetime", fields, batch_sizes)
+def generate_interval_case():
+ fields = [
+ DurationIntervalType('f1', 's'),
+ DurationIntervalType('f2', 'ms'),
+ DurationIntervalType('f3', 'us'),
+ DurationIntervalType('f4', 'ns'),
+ YearMonthIntervalType('f5'),
+ DayTimeIntervalType('f6'),
+ ]
+
+ batch_sizes = [7, 10]
+ return _generate_file("interval", fields, batch_sizes)
+
+
def generate_nested_case():
fields = [
ListType('list_nullable', get_field('item', 'int32')),
@@ -926,6 +1002,7 @@ def get_generated_json_files(tempdir=None, flight=False):
generate_primitive_case([0, 0, 0], name='primitive_zerolength'),
generate_decimal_case(),
generate_datetime_case(),
+ generate_interval_case(),
generate_nested_case(),
generate_dictionary_case().skip_category(SKIP_FLIGHT),
]
@@ -993,6 +1070,12 @@ class IntegrationRunner(object):
file_id = guid()[:8]
+ if ('JS' in (producer.name, consumer.name) and
+ "interval" in test_case.name):
+ print('TODO(ARROW-5239): Enable interval tests ' +
+ ' for JS once, JS supports them')
+ continue
+
# Make the random access file
producer_file_path = os.path.join(self.temp_dir, file_id + '_' +
name + '.json_as_file')