SQLite Forum

Colons in column names?
Login
Line 2012 in select.c is where the column names are made unique:

```
    /* Make sure the column name is unique.  If the name is not unique,
    ** append an integer to the name so that it becomes unique.
    */
    cnt = 0;
    while( zName && sqlite3HashFind(&ht, zName)!=0 ){
      nName = sqlite3Strlen30(zName);
      if( nName>0 ){
        for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
        if( zName[j]==':' ) nName = j;
      }
      zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
      if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
    }
    pCol->zName = zName;
```

So after having :4 any more duplicates get random numbers:

```
sqlite> create table x(x);
sqlite> insert into x values (1);
sqlite> select x,x,x,x,x,x.x,x,x,x,x from x;
x           x           x           x           x           x           x           x           x           x
----------  ----------  ----------  ----------  ----------  ----------  ----------  ----------  ----------  ----------
1           1           1           1           1           1           1           1           1           1
sqlite> select * from (select x,x,x,x,x,x.x,x,x,x,x from x);
x           x:1         x:2         x:3         x:4         x:1189230830  x:3251448327  x:2026156180  x:3116667273  x:3179755199
----------  ----------  ----------  ----------  ----------  ------------  ------------  ------------  ------------  ------------
1           1           1           1           1           1             1             1             1             1
```

Richard can probably answer why this is the case, but I would suspect that it is the old "Doctor, Doctor, it hurts when I poke myself in the eye!" to which the doctor replies "So don't poke yourself in the eye".

Note that the C API does return the correct metadata (column name, origin schema, origin table, origin column) if your framework asks for it.  For example my modified APSW which returns objects for rows and retrieves all available column metadata shows the following (note I have to mangle the returned column names so that they are properly formed unique attribute names):

```
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import apsw
>>> db = apsw.Connection()
>>> db.execute('create table x(x)')
>>> db.execute('insert into x values (1)')
>>> row = db.execute('select * from (select x,x,x,x,x,x,x,x,x from x)').fetchone()
>>> row
Row(x=1, x1=1, x2=1, x3=1, x4=1, x3711181259=1, x4278608952=1, x3945153751=1, x2452083445=1)
>>> row.x3711181259
1
>>> row._colnames
('x', 'x1', 'x2', 'x3', 'x4', 'x3711181259', 'x4278608952', 'x3945153751', 'x2452083445')
>>> row._orgnames
('x', 'x:1', 'x:2', 'x:3', 'x:4', 'x:3711181259', 'x:4278608952', 'x:3945153751', 'x:2452083445')
>>> row._colorig
('main.x.x', 'main.x.x', 'main.x.x', 'main.x.x', 'main.x.x', 'main.x.x', 'main.x.x', 'main.x.x', 'main.x.x')
```

_colnames are my mangled attribute names  
_orgnames are the original column names reported by SQLite  
_colorig  are the origin schema.table.column reported by SQLite

And for your original query:

```
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import apsw
>>> db = apsw.Connection()
>>> db.execute('create table t1(id);')
>>> db.execute('create table t2(id, t1Id);')
>>> db.execute('insert into t1 values (0); insert into t2 values (1,0);')
>>> row = db.execute('select * from (select t1.id, t2.id from t1 join t2 on t1.id = t2.t1Id);').fetchone()
>>> row
Row(id=0, id1=1)
>>> row._colnames
('id', 'id1')
>>> row._orgnames
('id', 'id:1')
>>> row._colorig
('main.t1.id', 'main.t2.id')
```