Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-12769

Arithmetic operators with temporal types create excessive column types

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Fixed
    • 10.1(EOL), 10.2(EOL), 10.3(EOL)
    • 10.3.1
    • Temporal Types
    • None

    Description

      CREATE OR REPLACE TABLE t1 AS SELECT
        TIMESTAMP'2004-01-01 10:20:30.123'+0 AS c1,
        TIME'10:20:30.123'+0 AS c2,
        DATE'2001-01-01'+0 AS c3;
      SHOW CREATE TABLE t1;
      

      +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
      | Table | Create Table                                                                                                                                                                                    |
      +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
      | t1    | CREATE TABLE `t1` (
        `c1` decimal(24,3) NOT NULL DEFAULT '0.000',
        `c2` decimal(15,3) NOT NULL DEFAULT '0.000',
        `c3` bigint(12) NOT NULL DEFAULT '0'
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
      +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
      

      The above types are excessive. This happens because Item::decimal_precision() treats temporal types as usual strings and uses max_length to calculate precision. This is wrong, because it does not exclude the formatting characters (dashes and semicolons).

      The correct integer part precision would be:

      • 14 for DATETIME and TIMESTAMP (YYYYMMDDhhmmss)
      • 7 for TIME (hhhmmss)
      • 8 for DATE (YYYYMMDD)

      Plus adds one digit to the integer part.

      The expected result for the above query would be:

      • 15 integer part digits for c1, which gives decimal(18,3)
      • 8 integer part digits for c2, which gives decimal(11,3)
      • 9 integer part digits for c3, which should give int(9), but DATE expression also do not set unsigned_flag properly, so int(10) would be ok until the unsigned_flag is fixed.

      Attachments

        Issue Links

          Activity

            bar Alexander Barkov created issue -
            bar Alexander Barkov made changes -
            Field Original Value New Value
            bar Alexander Barkov made changes -
            Description {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT
              TIMESTAMP'2004-01-01 10:20:30.123'+0 AS c1,
              TIME'10:20:30.123'+0 AS c2,
              DATE'2001-01-01'+0 AS c3;
            SHOW CREATE TABLE t1;
            {code}
            {noformat}
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            | Table | Create Table |
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            | t1 | CREATE TABLE `t1` (
              `c1` decimal(24,3) NOT NULL DEFAULT '0.000',
              `c2` decimal(15,3) NOT NULL DEFAULT '0.000',
              `c3` bigint(12) NOT NULL DEFAULT '0'
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            {noformat}

            The above types are excessive. This happens because {{Item::decimal_precision()}} treats temporal types as usual strings and uses {{max_length}} to calculate precision. This is wrong, because it does not exclude the formatting characters (dashes and semicolons).

            The correct {{decimal_precision()}} would be:
            - 14 for {{DATETIME}} and {{TIMESTAMP}} ({{YYYYMMDDhhmmss}})
            - 7 for {{TIME}} ({{hhhmmss}})
            - 8 for {{DATE}} ({{YYYYMMDD}})

            Plus adds one digit to the integer part.

            The expected result for the above query would be:
            - 15 integer part digits for {{c1}}, which gives {{decimal(18,3)}}
            - 8 integer part digits for {{c2}}, which gives {{decimal(11,3}}}}
            - 9 integer part digits for {{c3}}, which should give {{int(9)}}, but {{DATE}} expression also do not set {{unsigned_flag}} properly, so {{int(10)}} would be ok until the {{unsigned_flag}} is fixed.
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT
              TIMESTAMP'2004-01-01 10:20:30.123'+0 AS c1,
              TIME'10:20:30.123'+0 AS c2,
              DATE'2001-01-01'+0 AS c3;
            SHOW CREATE TABLE t1;
            {code}
            {noformat}
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            | Table | Create Table |
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            | t1 | CREATE TABLE `t1` (
              `c1` decimal(24,3) NOT NULL DEFAULT '0.000',
              `c2` decimal(15,3) NOT NULL DEFAULT '0.000',
              `c3` bigint(12) NOT NULL DEFAULT '0'
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            {noformat}

            The above types are excessive. This happens because {{Item::decimal_precision()}} treats temporal types as usual strings and uses {{max_length}} to calculate precision. This is wrong, because it does not exclude the formatting characters (dashes and semicolons).

            The correct integer part precision would be:
            - 14 for {{DATETIME}} and {{TIMESTAMP}} ({{YYYYMMDDhhmmss}})
            - 7 for {{TIME}} ({{hhhmmss}})
            - 8 for {{DATE}} ({{YYYYMMDD}})

            Plus adds one digit to the integer part.

            The expected result for the above query would be:
            - 15 integer part digits for {{c1}}, which gives {{decimal(18,3)}}
            - 8 integer part digits for {{c2}}, which gives {{decimal(11,3}}}}
            - 9 integer part digits for {{c3}}, which should give {{int(9)}}, but {{DATE}} expression also do not set {{unsigned_flag}} properly, so {{int(10)}} would be ok until the {{unsigned_flag}} is fixed.
            bar Alexander Barkov made changes -
            Description {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT
              TIMESTAMP'2004-01-01 10:20:30.123'+0 AS c1,
              TIME'10:20:30.123'+0 AS c2,
              DATE'2001-01-01'+0 AS c3;
            SHOW CREATE TABLE t1;
            {code}
            {noformat}
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            | Table | Create Table |
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            | t1 | CREATE TABLE `t1` (
              `c1` decimal(24,3) NOT NULL DEFAULT '0.000',
              `c2` decimal(15,3) NOT NULL DEFAULT '0.000',
              `c3` bigint(12) NOT NULL DEFAULT '0'
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            {noformat}

            The above types are excessive. This happens because {{Item::decimal_precision()}} treats temporal types as usual strings and uses {{max_length}} to calculate precision. This is wrong, because it does not exclude the formatting characters (dashes and semicolons).

            The correct integer part precision would be:
            - 14 for {{DATETIME}} and {{TIMESTAMP}} ({{YYYYMMDDhhmmss}})
            - 7 for {{TIME}} ({{hhhmmss}})
            - 8 for {{DATE}} ({{YYYYMMDD}})

            Plus adds one digit to the integer part.

            The expected result for the above query would be:
            - 15 integer part digits for {{c1}}, which gives {{decimal(18,3)}}
            - 8 integer part digits for {{c2}}, which gives {{decimal(11,3}}}}
            - 9 integer part digits for {{c3}}, which should give {{int(9)}}, but {{DATE}} expression also do not set {{unsigned_flag}} properly, so {{int(10)}} would be ok until the {{unsigned_flag}} is fixed.
            {code:sql}
            CREATE OR REPLACE TABLE t1 AS SELECT
              TIMESTAMP'2004-01-01 10:20:30.123'+0 AS c1,
              TIME'10:20:30.123'+0 AS c2,
              DATE'2001-01-01'+0 AS c3;
            SHOW CREATE TABLE t1;
            {code}
            {noformat}
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            | Table | Create Table |
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            | t1 | CREATE TABLE `t1` (
              `c1` decimal(24,3) NOT NULL DEFAULT '0.000',
              `c2` decimal(15,3) NOT NULL DEFAULT '0.000',
              `c3` bigint(12) NOT NULL DEFAULT '0'
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
            +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
            {noformat}

            The above types are excessive. This happens because {{Item::decimal_precision()}} treats temporal types as usual strings and uses {{max_length}} to calculate precision. This is wrong, because it does not exclude the formatting characters (dashes and semicolons).

            The correct integer part precision would be:
            - 14 for {{DATETIME}} and {{TIMESTAMP}} ({{YYYYMMDDhhmmss}})
            - 7 for {{TIME}} ({{hhhmmss}})
            - 8 for {{DATE}} ({{YYYYMMDD}})

            Plus adds one digit to the integer part.

            The expected result for the above query would be:
            - 15 integer part digits for {{c1}}, which gives {{decimal(18,3)}}
            - 8 integer part digits for {{c2}}, which gives {{decimal(11,3)}}
            - 9 integer part digits for {{c3}}, which should give {{int(9)}}, but {{DATE}} expression also do not set {{unsigned_flag}} properly, so {{int(10)}} would be ok until the {{unsigned_flag}} is fixed.
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            Fix Version/s 10.3.1 [ 22532 ]
            Fix Version/s 10.3 [ 22126 ]
            Resolution Fixed [ 1 ]
            Status Open [ 1 ] Closed [ 6 ]

            The patch for MDEV-12770 fixed this problem.

            bar Alexander Barkov added a comment - The patch for MDEV-12770 fixed this problem.
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 80704 ] MariaDB v4 [ 152139 ]

            People

              bar Alexander Barkov
              bar Alexander Barkov
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.