cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

Is this valid sql? "select count(*) usertable;"

Open jesterjaimi opened this issue 7 years ago • 2 comments

Describe the problem

More of a syntax question...

Ran the following statement without a from...

select count(*) usertable;

got the following.

root@localhost:26257/ycsb> select count(*) usertable; usertable +-----------+ 1

Seems rather liberal, but perhaps this is valid sql...

To Reproduce

select count(*) usertable; If possible, provide steps to reproduce the behavior:

Environment:

  • CockroachDB version v19.1.3
  • Server OS: Mac OSX 10.13.6
  • Client app [e.g. cockroach sql, JDBC, ...]

jesterjaimi avatar Jul 24 '19 14:07 jesterjaimi

Hey @jesterjaimi

The SELECT COUNT(*) is using an implicit AS in your query, and since a table is not being selected from, there's a "unit" table that is implicitly using a column named "usertable" with value of 1. You can see below that the first and second queries give the same result, versus the correct syntax for the count from table "usertable".

root@:26257/ycsb> SELECT COUNT(*) usertable;                                    
  usertable  
+-----------+
          1  
(1 row)

Time: 455µs

root@:26257/ycsb> SELECT COUNT(*) AS usertable;
  usertable  
+-----------+
          1  
(1 row)

Time: 511µs

root@:26257/ycsb> SELECT COUNT(*) FROM usertable;
  count  
+-------+
  10000  
(1 row)

Time: 6.467ms

Let me know if there are any other questions.

Cheers, Ricardo

ricardocrdb avatar Jul 24 '19 15:07 ricardocrdb

Hey @jesterjaimi

Did you have any other questions on this issue?

Cheers, Ricardo

ricardocrdb avatar Jul 30 '19 19:07 ricardocrdb