Skip to content

Commit

Permalink
changed array result to object result in dual expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedadiyan committed Jan 1, 2024
1 parent 23c31df commit 14cd501
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
12 changes: 12 additions & 0 deletions plsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type (
wg sync.WaitGroup
singletonExecutions map[string]any
postProcessors []func() error
dual bool
options struct {
wrapped bool
postgresEscapingDialect bool
Expand Down Expand Up @@ -499,6 +500,7 @@ func BuilFromAliasedTable(query *Query, as string, expr sqlparser.SimpleTableExp
case nil:
{
if qualifier == "" && tableName == "dual" {
query.dual = true
array, err := AsArray(query.data)
if err != nil {
return err
Expand Down Expand Up @@ -1607,6 +1609,16 @@ func (query *Query) Exec() (result any, err error) {
err = r.(error)
}
}()
if query.dual {
rs, err := ExecSelect(query, query.from)
if err != nil {
return nil, err
}
if len(rs) == 0 {
return nil, nil
}
return rs[0], nil
}
slice := make([]any, 0)
for _, current := range query.from {
switch current := current.(type) {
Expand Down
42 changes: 21 additions & 21 deletions plsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,127 +105,127 @@ func TestCteWithDimensionSelector(t *testing.T) {
}

func TestSubQuery(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main`
Tester(cmd, expected, t)
}

func TestWhere(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id = 1`
Tester(cmd, expected, t)
}

func TestBinaryAnd(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id = 1 AND full_name = 'Pouya Vedadiyan'`
Tester(cmd, expected, t)
}

func TestBinaryOr(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id = 1 OR full_name = 'imaginary friend'`
Tester(cmd, expected, t)
}

func TestBinaryMinus(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id = 2-1 OR full_name = 'imaginary friend'`
Tester(cmd, expected, t)
}

func TestBinaryPlus(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id = 1+1-1 OR full_name = 'imaginary friend'`
Tester(cmd, expected, t)
}

func TestBinaryMultiply(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id = 1*1 OR full_name = 'imaginary friend'`
Tester(cmd, expected, t)
}

func TestBinaryDivide(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id = 1/1 OR full_name = 'imaginary friend'`
Tester(cmd, expected, t)
}

func TestIs(t *testing.T) {
expected := "[map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where email IS NULL`
Tester(cmd, expected, t)
}

func TestIsNot(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id IS NOT NULL`
Tester(cmd, expected, t)
}

func TestIn(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id IN (1,2)`
Tester(cmd, expected, t)
}

func TestNotIn(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id NOT IN (10,20)`
Tester(cmd, expected, t)
}

func TestGt(t *testing.T) {
expected := "[map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where profile.height > 170`
Tester(cmd, expected, t)
}

func TestGte(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where profile.height >= 170`
Tester(cmd, expected, t)
}

func TestLt(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where profile.height < 180`
Tester(cmd, expected, t)
}

func TestLte(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where profile.height <= 180`
Tester(cmd, expected, t)
}

func TestBetween(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where profile.height BETWEEN 169 AND 181`
Tester(cmd, expected, t)
}

func TestNotBetween(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]] map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]] map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where profile.height NOT BETWEEN 181 AND 191`
Tester(cmd, expected, t)
}

func TestLike(t *testing.T) {
expected := "[map[data:[map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:Pouya Vedadiyan harcoded_value:HARD CODED id:1 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where email LIKE '%@%'`
Tester(cmd, expected, t)
}

func TestNotLike(t *testing.T) {
expected := "[map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where email NOT LIKE '%@%'`
Tester(cmd, expected, t)
}

func TestNotEqual(t *testing.T) {
expected := "[map[data:[map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]]"
expected := "[map[data:map[full_name:imaginary friend harcoded_value:HARD CODED id:2 liked:doom total_likes:3]]]"
cmd := `WITH Main AS (SELECT * FROM "root.data") SELECT (SELECT id, full_name, 'HARD CODED' AS harcoded_value, "likes[0].name" AS liked, SCOPED.COUNT(likes) AS total_likes) AS data FROM Main where id != 1`
Tester(cmd, expected, t)
}
Expand Down

0 comments on commit 14cd501

Please sign in to comment.