Different column type when using UNION ALL
(1) By anonymous on 2023-11-30 13:33:08 [source]
There is a change in behavior with nuget package System.Data.SQLite.Core between version 1.0.117 and 1.0.118.
See the sample. With version 1.0.117 the type of ResultTable.ResultColumn is REAL (which seems to be correct), but with version 1.0.118 the type is NUM.
Is this a bug or intended change? It this was intended is there a way to force REAL type on the resulting column?
Here is the sample code (C#/.NET 8):
using System.Data.SQLite;
var db = new SQLiteConnection("Data Source=:memory:");
db.Open();
var cmd = db.CreateCommand();
cmd.CommandText = """
CREATE TABLE SourceTable1 (ColumnName REAL);
CREATE TABLE SourceTable2 (ColumnName REAL);
CREATE TABLE ResultTable AS
SELECT CAST(ColumnName AS REAL) as ResultColumn FROM SourceTable1
UNION ALL
SELECT CAST(ColumnName AS REAL) as ResultColumn FROM SourceTable2;
""";
cmd.ExecuteNonQuery();
cmd = db.CreateCommand();
cmd.CommandText = """
PRAGMA table_info(ResultTable);
""";
var reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine(string.Join(' ', reader[0], reader[1], reader[2], reader[3]));
(2) By Holger J (holgerj) on 2023-12-01 15:09:10 in reply to 1 [link] [source]
https://www.sqlite.org/datatype3.html
A column with REAL affinity behaves like a column with NUMERIC affinity except that it forces integer values into floating point representation. (As an internal optimization, small floating point values with no fractional component and stored in columns with REAL affinity are written to disk as integers in order to take up less space and are automatically converted back into floating point as the value is read out. This optimization is completely invisible at the SQL level and can only be detected by examining the raw bits of the database file.)