SQLite User Forum

Comments in column names
Login

Comments in column names

(1) By anonymous on 2023-03-22 16:17:05 [link] [source]

Hi, I know column names aren't guaranteed unless explicitly stated, but I find this a bit weird:

.header on
select 'a' /* abc */, 'b', /*abc*/ 'c';

'a' /* abc */|'b'|'c'
a|b|c

(2) By Larry Brasfield (larrybr) on 2023-03-22 17:01:33 in reply to 1 [link] [source]

What do you believe would be less weird?

A more intriguing question (to me): How much care should go into devising result column names for those query writers who do not really care what the name are?

(3) By anonymous on 2023-03-22 17:44:06 in reply to 2 [link] [source]

I would expect it to be less weird if comments had NO impact on SQL results, even header names.

(4.1) By Aask (AAsk1902) on 2023-03-22 18:42:18 edited from 4.0 in reply to 2 [link] [source]

What do you believe would be less weird?

sqlite> .mode box
sqlite> select 'a' /* abc */, 'b', /*abc*/ 'c';
┌───────────────┬─────┬─────┐
│ 'a' /* abc */ │ 'b' │ 'c' │
├───────────────┼─────┼─────┤
│ a             │ b   │ c   │
└───────────────┴─────┴─────┘

1 Perhaps consistency would be less weird?

  • The trailing comment on the first column is retained as part of the column name.
  • The leading comment in the third column is disregarded.

2 Also, removing the single quotes from literal values (when used as an identifier) that are unnamed?

sqlite> create table t1 as select 'a' /* abc */, 'b', /*abc*/ 'c';
sqlite> .dump t1
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1("'a' /* abc */","'b'","'c'");
INSERT INTO t1 VALUES('a','b','c');
COMMIT;

(5) By Larry Brasfield (larrybr) on 2023-03-22 19:05:34 in reply to 4.0 [link] [source]

The present rule appears to be: Unless its column alias is given, the result column name is whatever expression yields the column value followed by other junk up to the next meaningful token, sans trailing whitespace.

This is simple to implement and understand, and makes it easy for casual, interactive query writers to correlate what is (or might be) seen as column headers to what they wrote.

I notice that nobody has undertaken an answer to my 2nd question. I suspect the answer is obvious: No further attention is needed. Certainly, there will not be some complicated set of rules devised, implemented and documented.

Perhaps, in some distant future, if given this query, select 2+3 /* A wee arithmetic demo. And a short story. /, 5 / is the Answer */; , the header will be formatted prettily and grammar-checked.

(6) By Aask (AAsk1902) on 2023-03-22 19:39:15 in reply to 5 [link] [source]

Perhaps, in some distant future, if given this query, select 2+3 /* A wee arithmetic demo. And a short story. /, 5 / is the Answer */; , the header will be formatted prettily and grammar-checked.

Not likely ... that would break baclward compatibility.

(7.1) By Stephan Beal (stephan) on 2023-03-22 19:41:56 edited from 7.0 in reply to 6 [link] [source]

Not likely ... that would break baclward compatibility.

Not strictly speaking because the current behavior is explicitly unreliable if "AS" is not used (either explicitly or implicitly) to name the columns.

(8) By Larry Brasfield (larrybr) on 2023-03-22 19:45:09 in reply to 6 [source]

... would break baclward compatibility.

The result column naming is undocumented. Backward compatibility for undocumented behavior is not something users should rely upon.

I expect likelihood is more driven by the existence of more valuable feature improvements.