Brian Aker wrote:
Hi!

On Dec 29, 2008, at 6:39 AM, Jay Pipes wrote:

a) The DATETIME data type should now be able to support MICROSECOND precision in the same 8-byte storage requirements. I've added routines for packing and unpacking 64-bit integers with all the needed parts of the date, including microseconds.

What method are you using?

I created the following two methods for DATETIME:

They work with a new structure similar to the current DRIZZLE_TIME struct called DRIZZLE_TEMPORAL:

/**
 * Structure which is used to represent temporal values inside Drizzle.
 *
 * This struct represents temporal data in both the Drizzle date system and
 * the UNIX Epoch system.  Therefore, it handles:
 *
 * - years in the range 0000-9999
 * - months in the range 1-12
 * - days in the range 1-31
 * - hours in the range 0-59
 * - minutes in the range 0-59
 * - seconds in the range 0-61 to account for leap seconds
 * - seconds since Epoch
 * - microseconds in the range 0 - 999,999
 * - nanoseconds in the range 0 - 999,999,999
 */
typedef struct st_drizzle_temporal
{
  enum enum_drizzle_temporal_type type;
  int years;
  int months;
  int days;
  int hours;
  int minutes;
  int seconds;
  time_t epoch_seconds;
  uint32_t useconds;
  uint32_t nseconds;
} DRIZZLE_TEMPORAL;

/**
 * Helper function to pack a DRIZZLE_TEMPORAL_DATETIME into
 * a uint64_t
 *
 * Returns a bool of whether the packing was
 * done correctly.
 *
 * @param source to pack from
 * @param destination uint64_t to pack into
 */
static bool pack_datetime_to_uint64_t(const DRIZZLE_TEMPORAL *from, uint64_t *to)
{
  /*
   * This data type is packed as follows:
   *
   * bits:
   *
   * | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
   * | YY | YY | YY | YY | YY | YY | YY | YY |
   * | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
   * | YY | YY | YY | YY | YY | YY | MM | MM |
   * | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
   * | MM | MM | DD | DD | DD | DD | DD | HH |
   * | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
   * | HH | HH | HH | HH | mm | mm | mm | mm |
   * | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
   * | mm | mm | SS | SS | SS | SS | SS | SS |
   * | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
   * | uS | uS | uS | uS | uS | uS | uS | uS |
   * | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 |
   * | uS | uS | uS | uS | uS | uS | uS | uS |
   * | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
   * | uS | uS | uS | uS | TT | TT | TT | TT |
   *
   * Where:
   *
   *  YY == Year      (1 - 9999)
   *  MM == Month     (1 - 12)
   *  DD == Days      (1 - 31)
   *  HH == Hours     (0 - 59)
   *  mm == Minutes   (0 - 59)
   *  SS == Seconds   (0 - 61) to account for leap seconds
   *  uS == Microseconds (0 - 999,999)
   *  TT == Type      (1-4 currently, may change)
   */

  if (from->type != DRIZZLE_TEMPORAL_DATETIME)
    return false;

  uint64_t packed_int= from->type
                      + (from->useconds << 4)
                      + (from->seconds << 24)
                      + (from->minutes << 30)
                      + (from->hours << 36)
                      + (from->days << 41)
                      + (from->months << 46)
                      + (from->years << 50);

  *to= packed_int;
  return true;
}

/**
 * Helper function to unpack a uint64_t into
 * a DRIZZLE_TEMPORAL of type DRIZZLE_TEMPORAL_DATETIME.
 *
 * Returns a bool of whether the unpacking was
 * done correctly.
 *
 * @param source uint64_t to unpack
 * @param destination to unpack into
 */
static bool unpack_uint64_t_to_datetime(const uint64_t *from, DRIZZLE_TEMPORAL *to)
{
  /*
   * This data type is packed as follows:
   *
   * bits:
   *
   * | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
   * | YY | YY | YY | YY | YY | YY | YY | YY |
   * | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
   * | YY | YY | YY | YY | YY | YY | MM | MM |
   * | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
   * | MM | MM | DD | DD | DD | DD | DD | HH |
   * | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
   * | HH | HH | HH | HH | mm | mm | mm | mm |
   * | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
   * | mm | mm | SS | SS | SS | SS | SS | SS |
   * | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
   * | uS | uS | uS | uS | uS | uS | uS | uS |
   * | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 |
   * | uS | uS | uS | uS | uS | uS | uS | uS |
   * | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
   * | uS | uS | uS | uS | TT | TT | TT | TT |
   *
   * Where:
   *
   *  YY == Year      (1 - 9999)
   *  MM == Month     (1 - 12)
   *  DD == Days      (1 - 31)
   *  HH == Hours     (0 - 59)
   *  mm == Minutes   (0 - 59)
   *  SS == Seconds   (0 - 61) to account for leap seconds
   *  uS == Microseconds (0 - 999,999)
   *  TT == Type      (1-4 currently, may change)
   */
  static const BITS_FOR_YEAR= 14;
  static const BITS_FOR_MONTH= 4;
  static const BITS_FOR_DAYS= 5;
  static const BITS_FOR_HOURS= 5;
  static const BITS_FOR_MINUTES= 6;
  static const BITS_FOR_SECONDS= 6;
  static const BITS_FOR_USECONDS= 20;
  static const BITS_FOR_TYPE= 4;

  uint32_t tmp_years= from >> 50;
  uint32_t tmp_months= (from >> 46) - ((from >> 50) * (2**BITS_FOR_MONTH));
  uint32_t tmp_days= (from >> 41) - ((from >> 46) * (2**BITS_FOR_DAYS));
  uint32_t tmp_hours= (from >> 36) - ((from >> 41) * (2**BITS_FOR_HOURS));
uint32_t tmp_minutes= (from >> 30) - ((from >> 36) * (2**BITS_FOR_MINUTES)); uint32_t tmp_seconds= (from >> 24) - ((from >> 30) * (2**BITS_FOR_SECONDS)); uint32_t tmp_useconds= (from >> 4) - ((from >> 24) * (2**BITS_FOR_USECONDS)); enum drizzle_temporal_type tmp_type= from - ((from >> 4) * (2**BITS_FOR_TYPE));

  if (tmp_type != DRIZZLE_TEMPORAL_DATETIME)
    return false;

  /* Construct the DRIZZLE_TEMPORAL */
  to->years= tmp_years;
  to->months= tmp_months;
  to->days= tmp_days;
  to->hours= tmp_hours;
  to->minutes= tmp_minutes;
  to->seconds= tmp_seconds;
  to->useconds= tmp_useconds;

if (tmp_years >= DRIZZLE_MIN_YEARS_EPOCH && tmp_years <= DRIZZLE_MAX_YEARS_EPOCH)
  {
    /* Add timestamp fields for timestamp-like values */
    time_t tmp_epoch_seconds;

    bool res= temporal_to_time_t(to, &tmp_epoch_seconds);
    if (res)
    {
      to->epoch_seconds= tmp_epoch_seconds;
    }
  }
  return true;
}

The above routines would, of course, be used to pack and unpack into the native integer format used by Drizzle and MySQL, which is cross-architecture compatible for storage via the drizzled/korr.h macros.

NOTE: The code above is untested! It is a piece of a stand-alone library that can be used to translate to and from DRIZZLE_TEMPORAL and standard time types like time_t, timeval, and timespec, and other standard types like uint64_t. In addition, the str_to_time and str_to_datetime functions from mysys/my_time.cc have been dramatically simplified and code-paths shortened.

I'll be done with the small unit testing piece today and will have cleaned up any errors by then.

b) I have the workings of a new data type, called TIMESTAMP_HIRES (or whatever, I don't really care about the name), which is an 8 byte column type which stores seconds from Epoch along with a nanosecond precision (basically, it's a storable struct timespec.)

Is it compatible with gettimeofday?

Completely.


Does anyone know why Field_timestamp and Field_datetime inherit from Field_str? I've seen no indication in the comments as to why this is the case... I'd like to get some explanation as to why this is the case before I proceed.

Just for a couple of bits of class inheritance. At one point in time I had a 16byte INT class for instance that I used for UUID and IPV6. It was there just to keep me from copy/paste.

OK, good to know. I assume you're not opposed to the idea of me scrapping the Field_str inheritance for DATE, DATETIME, and TIMESTAMP? With only UTF8 character sets, I don't see any need to inherit from Field_str.

Cheers,

-jay

_______________________________________________
Mailing list: https://launchpad.net/~drizzle-discuss
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~drizzle-discuss
More help   : https://help.launchpad.net/ListHelp

Reply via email to