DROP TABLE IF EXISTS t1;
|
CREATE TABLE t1 (val bit(1));
|
INSERT INTO t1 VALUES (0);
|
DROP TABLE IF EXISTS t2;
|
CREATE TABLE t2 AS SELECT COALESCE(val, 1) AS c FROM t1;
|
SELECT * FROM t2;
|
SHOW CREATE TABLE t2;
|
returns
+------+
|
| c |
|
+------+
|
| 0 |
|
+------+
|
+-------+--------------------------------------------------------------------------------------------+
|
| Table | Create Table |
|
+-------+--------------------------------------------------------------------------------------------+
|
| t2 | CREATE TABLE `t2` (
|
`c` decimal(1,0) DEFAULT NULL
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
|
+-------+--------------------------------------------------------------------------------------------+
|
Now if I run "mysql --column-type-info test" and execute this query:
SELECT COALESCE(val, 1) FROM t1;
|
it returns the following result and metadata:
+------------------+
|
| COALESCE(val, 1) |
|
+------------------+
|
| 0 |
|
+------------------+
|
Field 1: `COALESCE(val, 1)`
|
...
|
Type: VAR_STRING
|
Collation: binary (63)
|
Length: 1
|
Max_length: 1
|
Decimals: 31
|
Flags: UNSIGNED BINARY
|
Notice, COALESCE(val,1) positions itself as:
- DECIMAL(1,0) in CREATE TABLE...SELECT
- VARBINARY(1) in metadata returned by SELECT
The expected result is to return the same data type in both cases
Note, a UNION between a BIT column and an INT literal creates a column of type VARBINARY(20):
DROP TABLE IF EXISTS t1, t2;
|
CREATE TABLE t1 (val BIT(1));
|
CREATE TABLE t2 AS SELECT val FROM t1 UNION SELECT 1;
|
SHOW CREATE TABLE t2;
|
+-------+-----------------------------------------------------------------------------------------------+
|
| Table | Create Table |
|
+-------+-----------------------------------------------------------------------------------------------+
|
| t2 | CREATE TABLE `t2` (
|
`val` varbinary(20) DEFAULT NULL
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
|
+-------+-----------------------------------------------------------------------------------------------+
|
which is closer to what metadata returns for COALESCE(val,1) rather than what CREATE TABLE..SELECT returns for COALESCE(val,1).
Summary:
- SELECT bit_column FROM t1 - returns a string-alike value with BIT type in metadata
- SELECT bit_column FROM t1 UNION SELECT 1 – returns a number-alike value with VAR_STRING data type in metadata
- SELECT COALESCE(bit_column,1) FROM t1 – returns a number-alike value with VAR_STRING metadata
- CREATE TABLE t2 AS SELECT val FROM t1 UNION SELECT 1; – creates a column of type VARBINARY(20), with number-alike values
- CREATE TABLE t2 AS SELECT COALESCE(val,1) FROM t1; – creates a column of type DECIMAL(1,0)
{"report":{"fcp":860.1000001430511,"ttfb":129.70000004768372,"pageVisibility":"visible","entityId":53418,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"a6a7e27e-ec1f-4740-b100-1fa871776f3a","navigationType":0,"readyForUser":923,"redirectCount":0,"resourceLoadedEnd":965.3000001907349,"resourceLoadedStart":135,"resourceTiming":[{"duration":260.80000019073486,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":135,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":135,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":395.80000019073486,"responseStart":0,"secureConnectionStart":0},{"duration":260.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":135.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":135.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":395.80000019073486,"responseStart":0,"secureConnectionStart":0},{"duration":269.2999999523163,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":135.30000019073486,"connectEnd":135.30000019073486,"connectStart":135.30000019073486,"domainLookupEnd":135.30000019073486,"domainLookupStart":135.30000019073486,"fetchStart":135.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":135.30000019073486,"responseEnd":404.60000014305115,"responseStart":404.60000014305115,"secureConnectionStart":135.30000019073486},{"duration":294.2000000476837,"initiatorType":"script","name":"https://jira.mariadb.org/s/099b33461394b8015fc36c0a4b96e19f-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":135.60000014305115,"connectEnd":135.60000014305115,"connectStart":135.60000014305115,"domainLookupEnd":135.60000014305115,"domainLookupStart":135.60000014305115,"fetchStart":135.60000014305115,"redirectEnd":0,"redirectStart":0,"requestStart":135.60000014305115,"responseEnd":429.80000019073486,"responseStart":429.80000019073486,"secureConnectionStart":135.60000014305115},{"duration":297.7999999523163,"initiatorType":"script","name":"https://jira.mariadb.org/s/94c15bff32baef80f4096a08aceae8bc-CDN/lu2bu7/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":135.70000004768372,"connectEnd":135.70000004768372,"connectStart":135.70000004768372,"domainLookupEnd":135.70000004768372,"domainLookupStart":135.70000004768372,"fetchStart":135.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":135.70000004768372,"responseEnd":433.5,"responseStart":433.5,"secureConnectionStart":135.70000004768372},{"duration":298.39999985694885,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":135.80000019073486,"connectEnd":135.80000019073486,"connectStart":135.80000019073486,"domainLookupEnd":135.80000019073486,"domainLookupStart":135.80000019073486,"fetchStart":135.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":135.80000019073486,"responseEnd":434.2000000476837,"responseStart":434.2000000476837,"secureConnectionStart":135.80000019073486},{"duration":298.89999985694885,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":135.80000019073486,"connectEnd":135.80000019073486,"connectStart":135.80000019073486,"domainLookupEnd":135.80000019073486,"domainLookupStart":135.80000019073486,"fetchStart":135.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":135.80000019073486,"responseEnd":434.7000000476837,"responseStart":434.7000000476837,"secureConnectionStart":135.80000019073486},{"duration":299.40000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bu7/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":135.90000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":135.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":435.30000019073486,"responseStart":0,"secureConnectionStart":0},{"duration":299.30000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":136,"connectEnd":136,"connectStart":136,"domainLookupEnd":136,"domainLookupStart":136,"fetchStart":136,"redirectEnd":0,"redirectStart":0,"requestStart":136,"responseEnd":435.30000019073486,"responseStart":435.30000019073486,"secureConnectionStart":136},{"duration":299.7000000476837,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bu7/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":136.20000004768372,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":136.20000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":435.90000009536743,"responseStart":0,"secureConnectionStart":0},{"duration":299.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/3339d87fa2538a859872f2df449bf8d0-CDN/lu2bu7/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":136.40000009536743,"connectEnd":136.40000009536743,"connectStart":136.40000009536743,"domainLookupEnd":136.40000009536743,"domainLookupStart":136.40000009536743,"fetchStart":136.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":136.40000009536743,"responseEnd":436,"responseStart":436,"secureConnectionStart":136.40000009536743},{"duration":473.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":141.80000019073486,"connectEnd":141.80000019073486,"connectStart":141.80000019073486,"domainLookupEnd":141.80000019073486,"domainLookupStart":141.80000019073486,"fetchStart":141.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":141.80000019073486,"responseEnd":615.3000001907349,"responseStart":615.3000001907349,"secureConnectionStart":141.80000019073486},{"duration":823.4000000953674,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":141.90000009536743,"connectEnd":141.90000009536743,"connectStart":141.90000009536743,"domainLookupEnd":141.90000009536743,"domainLookupStart":141.90000009536743,"fetchStart":141.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":141.90000009536743,"responseEnd":965.3000001907349,"responseStart":965.3000001907349,"secureConnectionStart":141.90000009536743},{"duration":343.90000009536743,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":618.5,"connectEnd":618.5,"connectStart":618.5,"domainLookupEnd":618.5,"domainLookupStart":618.5,"fetchStart":618.5,"redirectEnd":0,"redirectStart":0,"requestStart":618.5,"responseEnd":962.4000000953674,"responseStart":962.4000000953674,"secureConnectionStart":618.5}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":3,"responseStart":130,"responseEnd":138,"domLoading":133,"domInteractive":989,"domContentLoadedEventStart":989,"domContentLoadedEventEnd":1029,"domComplete":1814,"loadEventStart":1814,"loadEventEnd":1816,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":972.3000001907349},{"name":"bigPipe.sidebar-id.end","time":973.1000001430511},{"name":"bigPipe.activity-panel-pipe-id.start","time":973.3000001907349},{"name":"bigPipe.activity-panel-pipe-id.end","time":975.5},{"name":"activityTabFullyLoaded","time":1039.9000000953674}],"measures":[],"correlationId":"cde426a0f05fee","effectiveType":"4g","downlink":9,"rtt":0,"serverDuration":69,"dbReadsTimeInMs":9,"dbConnsTimeInMs":17,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}