featurebase/sql3/sql_test.go
seebs b7e9879526
forward-port tests from SQL1 tree (#2261)
This forward-ports a number of tests from the previous SQL
implementation. The porting is approximate in a number of ways,
and not all tests are implemented/tested yet.

In particular, several tests are currently disabled because
we don't support `limit n` constructs.

The tests that were primarily tests of the parser have been
brought forward as parser tests. One of them has been altered
to add parentheses, because our parser interprets
	fld1 between 1 and 3 and fld2 = 2
as:
	fld1 between (1 and 3) and (fld2 = 2)
which is invalid, while the old parser apparently interpreted it
as:
	(fld1 between 1 and 3) and (fld2 = 2)

We have not yet verified the SQL spec's requirements here, but
sqlite agrees with our old parser, not our new parser, so this
may be a regression.

The old tests expected an INNER JOIN to suppress duplicate
values. Our new code does not, which is consistent with other
SQL implementations. This is a change, but the old behavior
appears to have been wrong. (You can still suppress duplicate
values by specifying DISTINCT.)

In the previous implementations, a value like `count(*)` had
`count(*)` as its column name. In the new implementation,
it has an empty string as its column name.

Related to this, the prior implementation allowed you to
write
	select age, count(*) from grouper group by age having count > 1
but the new implementationt requires that to be spelled as
	having count(*) > 1

This is consistent with other SQL implementations, so I think
the new behavior is correct.

The behavior of SHOW COLUMNS and SHOW TABLES has changed, in
that the specific results returned are significantly different.
Perhaps more significantly, the old system spelled the former
query as SHOW FIELDS, rather than SHOW COLUMNS. This may be
considered a regression, in that `SHOW FIELDS` no longer works,
and we should consider whether any hypothetical users might
have been relying on the output of either of these. (I hope
not, the new output is much better.)

Some of the old tests (the ones in handler_test) were accommodated
by adding a couple of specific test cases to existing tests,
specifically:
	* handling timestamp values with `Z` rather than `+00:00`
	* a join with a WHERE clause referring to fields in both
	  source tables

We introduce a new "partial" comparison type, because there's
no way for a test of `SHOW TABLES` to contain a correct table
row, because `SHOW TABLES` includes timestamps from when tables
were created. I'm not sure this is the right way to do this.

We add corresponding changes to dax_test, because the DAX tree
tests against the SQL tests.

We change the returned types of field names and field types to
plain strings, ironically because DAX needs this -- the test code
in the DAX tree is getting them back as plain strings, rather
than as dax.FieldName and dax.BaseType.

The tests using `having` are commented out because they don't
seem to be working, a ticket has been filed for this.

Two of the tests that should return strings are instead returning
untranslated integer IDs, but only for DAX, not for the regular
SQL tests, and the `delete` test has been commented out for
DAX-specific errors. If we merge this, the next step is to
ticket those and address them separately.
2023-03-01 13:40:50 -06:00

166 lines
5 KiB
Go

// Copyright 2021 Molecula Corp. All rights reserved.
package sql3_test
import (
"fmt"
"log"
"sort"
"strings"
"testing"
"github.com/featurebasedb/featurebase/v3/dax"
"github.com/featurebasedb/featurebase/v3/sql3"
sql_test "github.com/featurebasedb/featurebase/v3/sql3/test"
"github.com/featurebasedb/featurebase/v3/sql3/test/defs"
"github.com/featurebasedb/featurebase/v3/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSQL_Execute(t *testing.T) {
c := test.MustRunCluster(t, 1)
defer c.Close()
svr := c.GetNode(0).Server
for i, test := range defs.TableTests {
t.Run(test.Name(i), func(t *testing.T) {
// Create a table with all field types.
if test.HasTable() {
_, _, _, err := sql_test.MustQueryRows(t, svr, test.CreateTable())
assert.NoError(t, err)
}
if test.HasTable() && test.HasData() {
// Populate fields with data.
_, _, _, err := sql_test.MustQueryRows(t, svr, test.InsertInto(t))
assert.NoError(t, err)
}
for i, sqltest := range test.SQLTests {
t.Run(sqltest.Name(i), func(t *testing.T) {
for _, sql := range sqltest.SQLs {
t.Run(fmt.Sprintf("sql-%s", sql), func(t *testing.T) {
log.Printf("SQL: %s", sql)
rows, headers, plan, err := sql_test.MustQueryRows(t, svr, sql)
// Check expected error instead of results.
if sqltest.ExpErr != "" {
if assert.Error(t, err) {
assert.Contains(t, err.Error(), sqltest.ExpErr)
}
return
}
require.NoError(t, err)
// Check headers.
assert.ElementsMatch(t, sqltest.ExpHdrs, headers)
// make a map of column name to header index
m := make(map[dax.FieldName]int)
for i := range headers {
m[headers[i].Name] = i
}
// TODO(pok) - this will become increasingly problematic as result column headers
// are not unique and can be empty
// Put the expRows in the same column order as the headers returned
// by the query.
exp := make([][]interface{}, len(sqltest.ExpRows))
for i := range sqltest.ExpRows {
exp[i] = make([]interface{}, len(headers))
for j := range sqltest.ExpHdrs {
targetIdx := m[sqltest.ExpHdrs[j].Name]
if sqltest.Compare != defs.ComparePartial {
assert.GreaterOrEqual(t, len(sqltest.ExpRows[i]), len(headers),
"expected row set has fewer columns than returned headers")
}
// if ExpRows[i] is short, that might be okay if we're doing a "partial"
// compare.
if len(sqltest.ExpRows[i]) > j {
exp[i][targetIdx] = sqltest.ExpRows[i][j]
}
}
}
if sqltest.SortStringKeys {
sortStringKeys(rows)
}
switch sqltest.Compare {
case defs.CompareExactOrdered:
assert.Equal(t, len(sqltest.ExpRows), len(rows))
assert.EqualValues(t, exp, rows)
case defs.CompareExactUnordered:
assert.Equal(t, len(sqltest.ExpRows), len(rows))
assert.ElementsMatch(t, exp, rows)
case defs.CompareIncludedIn:
assert.Equal(t, sqltest.ExpRowCount, len(rows))
for _, row := range rows {
assert.Contains(t, exp, row)
}
case defs.ComparePartial:
assert.LessOrEqual(t, len(sqltest.ExpRows), len(rows))
// Assert that every non-nil value in the row is found somewhere
// in the corresponding expected row.
for i, expRow := range exp {
// have we found everything in this row yet?
foundAll := false
for _, row := range rows {
maybeFound := true
for k, exp := range expRow {
if exp != nil {
if k > len(row) || row[k] != exp {
maybeFound = false
break
}
}
}
if maybeFound {
foundAll = true
break
}
}
if !foundAll {
t.Errorf("expected row %d: couldn't find any result row matching all its values %#v", i, expRow)
}
}
}
if sqltest.PlanCheck != nil {
err := sqltest.PlanCheck(plan)
require.NoError(t, err)
}
})
}
})
}
})
}
}
// sortStringKeys goes through an entire set of rows, and for any []string it
// finds, it orders the elements. This is obviously only useful in tests, and
// only in cases where we expect the elements to match, but we don't care what
// order they're in. It's basically the equivalent of assert.ElementsMatch(),
// but the way we use that on rows doesn't recurse down into the field values
// within each row.
func sortStringKeys(in [][]interface{}) {
for i := range in {
for j := range in[i] {
switch v := in[i][j].(type) {
case []string:
sort.Strings(v)
}
}
}
}
func TestInternalError(t *testing.T) {
e := sql3.NewErrInternal("foo")
if !strings.Contains(e.Error(), "test.go") {
t.Fatalf("internal error from *_test.go file should contain test.go string")
}
}