Skip to content

Commit

Permalink
Implement constant selection queries
Browse files Browse the repository at this point in the history
Signed-off-by: Stefano Scafiti <stefano.scafiti96@gmail.com>
  • Loading branch information
ostafen committed Dec 12, 2024
1 parent 52df5f2 commit 56e462f
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 166 deletions.
17 changes: 17 additions & 0 deletions embedded/sql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,23 @@ func TestQuery(t *testing.T) {
require.Equal(t, expectedRows[i].ValuesByPosition, row.ValuesByPosition)
}
})

t.Run("constant selection query", func(t *testing.T) {
_, err := engine.queryAll(
context.Background(),
nil,
"SELECT *",
nil,
)
require.ErrorContains(t, err, "SELECT * with no tables specified is not valid")

assertQueryShouldProduceResults(
t,
engine,
"SELECT 1, true, 'test'",
"SELECT * FROM (VALUES (1, true, 'test'))",
)
})
}

func TestJSON(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions embedded/sql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,19 @@ func TestSelectStmt(t *testing.T) {
expectedOutput []SQLStmt
expectedError error
}{
{
input: "SELECT 1, true, 'test'",
expectedOutput: []SQLStmt{
&SelectStmt{
targets: []TargetEntry{
{Exp: &Integer{1}},
{Exp: &Bool{true}},
{Exp: &Varchar{"test"}},
},
ds: &valuesDataSource{rows: []*RowSpec{{}}},
},
},
},
{
input: "SELECT id, title FROM table1",
expectedOutput: []SQLStmt{
Expand Down
4 changes: 4 additions & 0 deletions embedded/sql/proj_row_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func newProjectedRowReader(ctx context.Context, rowReader RowReader, tableAlias
return nil, err
}

if len(cols) == 0 {
return nil, fmt.Errorf("SELECT * with no tables specified is not valid")
}

for _, col := range cols {
targets = append(targets, TargetEntry{
Exp: &ColSelector{
Expand Down
10 changes: 10 additions & 0 deletions embedded/sql/sql_grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,16 @@ select_stmt: SELECT opt_distinct opt_targets FROM ds opt_indexon opt_joins opt_w
offset: $13,
}
}
|
SELECT opt_distinct opt_targets
{
$$ = &SelectStmt{
distinct: $2,
targets: $3,
ds: &valuesDataSource{rows: []*RowSpec{{}}},
}
}
;

opt_all:
{
Expand Down
Loading

0 comments on commit 56e462f

Please sign in to comment.