mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
* Fix formatting in CLI results with custom SQLResonse.UnmarshalJSON
When I started this, it was meant to be a quick fix to address the confusing
result formats we were seeing in the CLI. For example, all large integer values
were displayed in scientifc notation. This is because we were passing the result
types from JSON (in this case, float64) into pretty print. Similarly, `IDSets`
and `StringSets` where being printed using the default go Stringer for the types
[]int64 and []string respectively.
I started by writing a customer UnmarshalJSON() method for the `SQLResponse`
type. Part of this (the part which converts data types based on header types)
was already being used in dax tests, so this just formalizes that logic as part
of the `SQLResponse` type.
Then I realized that the sql3 tests (run against the `sql3` package) were
failing because sql3 is not actually returning the `IDSets` and `StringSets`
types. A future task is to formalize return types, define them, and modify sql3
to return them. Once that is done, we can remove the "typed" switch in the
`SQLResponse` json unmarshaller.
Another significant change is the modification to the `ExprDataType` interface:
```
type ExprDataType interface {
exprDataType()
TypeName() string
TypeDescription() string
TypeInfo() map[string]interface{}
}
```
I added two more methods in order to distinguish between a type (`DECIMAL`), its
description (`DECIMAL(2)`), and its type info (`"scale": int64(2)`). Currently,
the description can be used as the field definition in a CREATE TABLE statement,
but we may want to re-think that. Also, Decimal is the only type currently using
TypeInfo.
Finally, I tried to consilidate things around `dax.FieldType` instead of
comparing against parser types outside of sql3. We still have some sql3 parser
and planner types lurking about, but we can address those in future commits.
* Add some test coverage
* smoke test expected INT, now int
* minor fixes
* Introduce WireQueryResponse and related types
This also changes dax.FieldType to dax.BaseType.
* Populate WireQueryResponse correctly
Currently this is in the http handler, and in the queryer.
* Convert sql3 and dax tests to expect pilosa.WireQueryField in results
* fix PQL tests in the SQL defs
* Address a few of the skipped sql tests in dax
(cherry picked from commit f4385df2cf)
118 lines
3.4 KiB
Go
118 lines
3.4 KiB
Go
// Copyright 2021 Molecula Corp. All rights reserved.
|
|
package sql3_test
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/molecula/featurebase/v3/dax"
|
|
sql_test "github.com/molecula/featurebase/v3/sql3/test"
|
|
"github.com/molecula/featurebase/v3/sql3/test/defs"
|
|
"github.com/molecula/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, 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
|
|
}
|
|
|
|
// 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]
|
|
assert.GreaterOrEqual(t, len(sqltest.ExpRows[i]), len(headers),
|
|
"expected row set has fewer columns than returned headers")
|
|
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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
}
|
|
}
|