Skip to content

Commit cdd00cb

Browse files
committed
Implement constant selection queries
Signed-off-by: Stefano Scafiti <[email protected]>
1 parent 52df5f2 commit cdd00cb

File tree

7 files changed

+229
-166
lines changed

7 files changed

+229
-166
lines changed

embedded/sql/engine_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,6 +3153,23 @@ func TestQuery(t *testing.T) {
31533153
require.Equal(t, expectedRows[i].ValuesByPosition, row.ValuesByPosition)
31543154
}
31553155
})
3156+
3157+
t.Run("constant selection query", func(t *testing.T) {
3158+
_, err := engine.queryAll(
3159+
context.Background(),
3160+
nil,
3161+
"SELECT *",
3162+
nil,
3163+
)
3164+
require.ErrorContains(t, err, "SELECT * with no tables specified is not valid")
3165+
3166+
assertQueryShouldProduceResults(
3167+
t,
3168+
engine,
3169+
"SELECT 1, true, 'test'",
3170+
"SELECT * FROM (VALUES (1, true, 'test'))",
3171+
)
3172+
})
31563173
}
31573174

31583175
func TestJSON(t *testing.T) {

embedded/sql/parser_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,19 @@ func TestSelectStmt(t *testing.T) {
898898
expectedOutput []SQLStmt
899899
expectedError error
900900
}{
901+
{
902+
input: "SELECT 1, true, 'test'",
903+
expectedOutput: []SQLStmt{
904+
&SelectStmt{
905+
targets: []TargetEntry{
906+
{Exp: &Integer{1}},
907+
{Exp: &Bool{true}},
908+
{Exp: &Varchar{"test"}},
909+
},
910+
ds: &valuesDataSource{rows: []*RowSpec{{}}},
911+
},
912+
},
913+
},
901914
{
902915
input: "SELECT id, title FROM table1",
903916
expectedOutput: []SQLStmt{

embedded/sql/proj_row_reader.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func newProjectedRowReader(ctx context.Context, rowReader RowReader, tableAlias
3636
return nil, err
3737
}
3838

39+
if len(cols) == 0 {
40+
return nil, fmt.Errorf("SELECT * with no tables specified is not valid")
41+
}
42+
3943
for _, col := range cols {
4044
targets = append(targets, TargetEntry{
4145
Exp: &ColSelector{

embedded/sql/sql_grammar.y

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,16 @@ select_stmt: SELECT opt_distinct opt_targets FROM ds opt_indexon opt_joins opt_w
717717
offset: $13,
718718
}
719719
}
720+
|
721+
SELECT opt_distinct opt_targets
722+
{
723+
$$ = &SelectStmt{
724+
distinct: $2,
725+
targets: $3,
726+
ds: &valuesDataSource{rows: []*RowSpec{{}}},
727+
}
728+
}
729+
;
720730

721731
opt_all:
722732
{

0 commit comments

Comments
 (0)