Repository: genjidb/genji (go)

Issue:
database/sql ORDER BY error
Loving genji so far - thanks!

### What version of Genji are you using?
<pre>
v0.9.1-0.20210105060628-b0134220aac3
</pre>

### Does this issue reproduce with the latest release?

Yes

### What did you do?

When using the database/sql driver, adding an `ORDER BY x` clause (with or without a direction) to a filtered `SELECT` query returns an error.  The same `SELECT` with `ORDER BY` clause using the Genji API works as expected.

Minimal example:
https://play.golang.org/p/F5bfkULNCWd

### What did you expect to see?

An dump of a and b values, ordered by a.

### What did you see instead?

`ERROR 1: sql: expected 0 destination arguments in Scan, not 2` unless the `ORDER BY a` is removed.

### What Go version and environment are you using?

<pre>
$ go version
go version go1.15.6 darwin/amd64
</pre>

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/user/Library/Caches/go-build"
GOENV="/Users/user/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/user/Code/gocode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/user/Code/gocode"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.6/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/user/Code/gocode/src/github.com/hynd/genjitest/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/3r/_jv56wbj2xb6gwh9pkv9y62cg1_7cm/T/go-build810324326=/tmp/go-build -gno-record-gcc-switches -fno-common"
</pre></details>

Pull request:
Fix driver.Sql with Stream.Operator finder.
This fixes #338. 


Patch:
diff --git a/sql/driver/driver.go b/sql/driver/driver.go
index fde85b28..1340644a 100644
--- a/sql/driver/driver.go
+++ b/sql/driver/driver.go
@@ -273,18 +273,23 @@ func (s stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (drive
 	lastStmt := s.q.Statements[len(s.q.Statements)-1]
 
 	stmt, ok := lastStmt.(*planner.Statement)
-	if !ok {
+	if !ok || stmt.Stream.Op == nil {
 		return rs, nil
 	}
 
-	if stmt.Stream.Op == nil {
-		return rs, nil
-	}
+	// Search the ProjectOperator.
+	for op := stmt.Stream.First(); op != nil; op = op.GetNext() {
+		if po, ok := op.(*stream.ProjectOperator); ok {
+			if len(po.Exprs) == 0 {
+				break
+			}
+
+			rs.fields = make([]string, len(po.Exprs))
+			for i := range po.Exprs {
+				rs.fields[i] = fmt.Sprintf("%s", po.Exprs[i])
+			}
 
-	if po, ok := stmt.Stream.Op.(*stream.ProjectOperator); ok && len(po.Exprs) > 0 {
-		rs.fields = make([]string, len(po.Exprs))
-		for i := range po.Exprs {
-			rs.fields[i] = fmt.Sprintf("%s", po.Exprs[i])
+			return rs, nil
 		}
 	}
 


Test patch:
diff --git a/sql/driver/driver_test.go b/sql/driver/driver_test.go
index 1ae300ae..a55c7df0 100644
--- a/sql/driver/driver_test.go
+++ b/sql/driver/driver_test.go
@@ -70,6 +70,59 @@ func TestDriver(t *testing.T) {
 		require.Equal(t, 10, count)
 	})
 
+	t.Run("Multiple fields with ORDER BY", func(t *testing.T) {
+		rows, err := db.Query("SELECT a, c FROM test ORDER BY a")
+		require.NoError(t, err)
+		defer rows.Close()
+
+		var count int
+		var a int
+		var c foo
+		for rows.Next() {
+			err = rows.Scan(&a, Scanner(&c))
+			require.NoError(t, err)
+			require.Equal(t, count, a)
+			require.Equal(t, foo{Foo: "bar"}, c)
+			count++
+		}
+		require.NoError(t, rows.Err())
+		require.Equal(t, 10, count)
+	})
+
+	t.Run("Wilcards with ORDER BY", func(t *testing.T) {
+		rows, err := db.Query("SELECT * FROM test ORDER BY a")
+		require.NoError(t, err)
+		defer rows.Close()
+
+		var count int
+		var dt doctest
+		for rows.Next() {
+			err = rows.Scan(Scanner(&dt))
+			require.NoError(t, err)
+			require.Equal(t, doctest{count, []int{count + 1, count + 2, count + 3}, foo{Foo: "bar"}}, dt)
+			count++
+		}
+		require.NoError(t, rows.Err())
+		require.Equal(t, 10, count)
+	})
+
+	t.Run("Wilcards with ORDER BY and LIMIT", func(t *testing.T) {
+		rows, err := db.Query("SELECT * FROM test ORDER BY a LIMIT 5")
+		require.NoError(t, err)
+		defer rows.Close()
+
+		var count int
+		var dt doctest
+		for rows.Next() {
+			err = rows.Scan(Scanner(&dt))
+			require.NoError(t, err)
+			require.Equal(t, doctest{count, []int{count + 1, count + 2, count + 3}, foo{Foo: "bar"}}, dt)
+			count++
+		}
+		require.NoError(t, rows.Err())
+		require.Equal(t, 5, count)
+	})
+
 	t.Run("Multiple fields and wildcards", func(t *testing.T) {
 		rows, err := db.Query("SELECT a, a, *, b, c, * FROM test")
 		require.NoError(t, err)