Skip to content

Commit

Permalink
fix: classify SELECT DATABASE() as non-pure data query in isPureDataQ…
Browse files Browse the repository at this point in the history
…uery
  • Loading branch information
GaoYusong committed Sep 5, 2024
1 parent fafdd98 commit c1b9ab5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ func isPureDataQuery(n sql.Node) bool {
switch tn.Database().Name() {
case "mysql", "information_schema", "performance_schema", "sys":
return false
case "":
return false
}
}
return true
Expand Down
56 changes: 56 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/dolthub/go-mysql-server/enginetest"
"github.com/dolthub/go-mysql-server/enginetest/scriptgen/setup"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/go-mysql-server/sql/planbuilder"
"github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/vitess/go/sqltypes"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -98,3 +101,56 @@ func TestDuckBuilder_Select(t *testing.T) {

assert.NoError(t, mock.ExpectationsWereMet())
}

func TestIsPureDataQuery(t *testing.T) {
harness := NewDefaultDuckHarness()
harness.Setup(
setup.MydbData,
[]setup.SetupScript{
{
"CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255))",
},
})
ctx := enginetest.NewContext(harness)
engine, err := harness.NewEngine(t)
require.NoError(t, err)
tests := []struct {
name string
query string
expected bool
}{
{
name: "Simple SELECT query",
query: "SELECT * FROM users",
expected: true,
},
{
name: "Query from mysql system table",
query: "SELECT * FROM mysql.user",
expected: false,
},
{
name: "Query with system function",
query: "SELECT DATABASE()",
expected: false,
},
// {
// name: "Query with subquery from system table",
// query: "SELECT u.name, (SELECT COUNT(*) FROM mysql.user) FROM users u",
// expected: false,
// },
{
name: "Query from information_schema",
query: "SELECT * FROM information_schema.tables",
expected: false,
},
}
for _, tt := range tests {
parsed, _, err := planbuilder.Parse(ctx, engine.EngineAnalyzer().Catalog, tt.query)
require.NoError(t, err)

result := isPureDataQuery(parsed)
assert.Equal(t, tt.expected, result, "isPureDataQuery() for query '%s'", tt.query)
}

}

0 comments on commit c1b9ab5

Please sign in to comment.