and found a bug

Signed-off-by: Sugu Sougoumarane <ssougou@gmail.com>
This commit is contained in:
Sugu Sougoumarane 2019-06-16 21:57:28 -07:00
Родитель b54cc62707
Коммит a7f5814c63
4 изменённых файлов: 71 добавлений и 9 удалений

Просмотреть файл

@ -32,16 +32,26 @@ func TestAggregateTypes(t *testing.T) {
}
defer conn.Close()
exec(t, conn, "insert into aggr_test(id, val1, val2) values(1,'a',1), (2,'a',1), (3,'b',1), (4,'c',3), (5,'c',4)")
exec(t, conn, "insert into aggr_test(id, val1, val2) values(6,'d',null), (7,'e',null), (8,'e',1)")
exec(t, conn, "insert into aggr_test(id, val1, val2) values(1,'a',1), (2,'A',1), (3,'b',1), (4,'c',3), (5,'c',4)")
exec(t, conn, "insert into aggr_test(id, val1, val2) values(6,'d',null), (7,'e',null), (8,'E',1)")
qr := exec(t, conn, "select val1, count(distinct val2), count(*) from aggr_test group by val1")
if got, want := fmt.Sprintf("%v", qr.Rows), `[[VARBINARY("a") INT64(1) INT64(2)] [VARBINARY("b") INT64(1) INT64(1)] [VARBINARY("c") INT64(2) INT64(2)] [VARBINARY("d") INT64(0) INT64(1)] [VARBINARY("e") INT64(1) INT64(2)]]`; got != want {
if got, want := fmt.Sprintf("%v", qr.Rows), `[[VARCHAR("a") INT64(1) INT64(2)] [VARCHAR("b") INT64(1) INT64(1)] [VARCHAR("c") INT64(2) INT64(2)] [VARCHAR("d") INT64(0) INT64(1)] [VARCHAR("e") INT64(1) INT64(2)]]`; got != want {
t.Errorf("select:\n%v want\n%v", got, want)
}
qr = exec(t, conn, "select val1, sum(distinct val2), sum(val2) from aggr_test group by val1")
if got, want := fmt.Sprintf("%v", qr.Rows), `[[VARBINARY("a") DECIMAL(1) DECIMAL(2)] [VARBINARY("b") DECIMAL(1) DECIMAL(1)] [VARBINARY("c") DECIMAL(7) DECIMAL(7)] [VARBINARY("d") NULL NULL] [VARBINARY("e") DECIMAL(1) DECIMAL(1)]]`; got != want {
if got, want := fmt.Sprintf("%v", qr.Rows), `[[VARCHAR("a") DECIMAL(1) DECIMAL(2)] [VARCHAR("b") DECIMAL(1) DECIMAL(1)] [VARCHAR("c") DECIMAL(7) DECIMAL(7)] [VARCHAR("d") NULL NULL] [VARCHAR("e") DECIMAL(1) DECIMAL(1)]]`; got != want {
t.Errorf("select:\n%v want\n%v", got, want)
}
qr = exec(t, conn, "select val1, count(distinct val2) k, count(*) from aggr_test group by val1 order by k desc, val1")
if got, want := fmt.Sprintf("%v", qr.Rows), `[[VARCHAR("c") INT64(2) INT64(2)] [VARCHAR("a") INT64(1) INT64(2)] [VARCHAR("b") INT64(1) INT64(1)] [VARCHAR("e") INT64(1) INT64(2)] [VARCHAR("d") INT64(0) INT64(1)]]`; got != want {
t.Errorf("select:\n%v want\n%v", got, want)
}
qr = exec(t, conn, "select val1, count(distinct val2) k, count(*) from aggr_test group by val1 order by k desc, val1 limit 4")
if got, want := fmt.Sprintf("%v", qr.Rows), `[[VARCHAR("c") INT64(2) INT64(2)] [VARCHAR("a") INT64(1) INT64(2)] [VARCHAR("b") INT64(1) INT64(1)] [VARCHAR("e") INT64(1) INT64(2)]]`; got != want {
t.Errorf("select:\n%v want\n%v", got, want)
}
}

Просмотреть файл

@ -24,6 +24,7 @@ import (
"testing"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/vttest"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
@ -57,7 +58,7 @@ create table vstream_test(
create table aggr_test(
id bigint,
val1 varbinary(16),
val1 varchar(16),
val2 bigint,
primary key(id)
) Engine=InnoDB;
@ -145,6 +146,10 @@ create table t2_id4_idx(
Column: "id",
Name: "hash",
}},
Columns: []*vschemapb.Column{{
Name: "val1",
Type: sqltypes.VarChar,
}},
},
},
}

Просмотреть файл

@ -283,7 +283,6 @@ func (oa *orderedAggregate) pushAggr(pb *primitiveBuilder, expr *sqlparser.Alias
if len(funcExpr.Exprs) != 1 {
return nil, 0, fmt.Errorf("unsupported: only one expression allowed inside aggregates: %s", sqlparser.String(funcExpr))
}
var innerRC *resultColumn
var innerCol int
handleDistinct, innerAliased, err := oa.needDistinctHandling(pb, funcExpr, opcode)
if err != nil {
@ -295,7 +294,7 @@ func (oa *orderedAggregate) pushAggr(pb *primitiveBuilder, expr *sqlparser.Alias
}
// Push the expression that's inside the aggregate.
// The column will eventually get added to the group by and order by clauses.
innerRC, innerCol, _ = oa.input.PushSelect(pb, innerAliased, origin)
_, innerCol, _ = oa.input.PushSelect(pb, innerAliased, origin)
col, err := BuildColName(oa.input.ResultColumns(), innerCol)
if err != nil {
return nil, 0, err
@ -320,7 +319,7 @@ func (oa *orderedAggregate) pushAggr(pb *primitiveBuilder, expr *sqlparser.Alias
Alias: alias,
})
} else {
innerRC, innerCol, _ = oa.input.PushSelect(pb, expr, origin)
_, innerCol, _ = oa.input.PushSelect(pb, expr, origin)
oa.eaggr.Aggregates = append(oa.eaggr.Aggregates, engine.AggregateParams{
Opcode: opcode,
Col: innerCol,
@ -329,7 +328,7 @@ func (oa *orderedAggregate) pushAggr(pb *primitiveBuilder, expr *sqlparser.Alias
// Build a new rc with oa as origin because it's semantically different
// from the expression we pushed down.
rc = &resultColumn{alias: innerRC.alias, column: &column{origin: oa}}
rc = newResultColumn(expr, oa)
oa.resultColumns = append(oa.resultColumns, rc)
return rc, len(oa.resultColumns) - 1, nil
}

Просмотреть файл

@ -846,6 +846,54 @@
}
}
# order by count distinct
"select col1, count(distinct col2) k from user group by col1 order by k"
{
"Original": "select col1, count(distinct col2) k from user group by col1 order by k",
"Instructions": {
"Opcode": "MemorySort",
"MaxRows": null,
"OrderBy": [
{
"Col": 1,
"Desc": false
}
],
"Input": {
"HasDistinct": true,
"Aggregates": [
{
"Opcode": "count_distinct",
"Col": 1,
"Alias": "k"
}
],
"Keys": [
0
],
"Input": {
"Opcode": "SelectScatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"Query": "select col1, col2 from user group by col1, col2 order by col1 asc, col2 asc",
"FieldQuery": "select col1, col2 from user where 1 != 1 group by col1, col2",
"OrderBy": [
{
"Col": 0,
"Desc": false
},
{
"Col": 1,
"Desc": false
}
]
}
}
}
}
# scatter aggregate group by aggregate function
" select count(*) b from user group by b"
"group by expression cannot reference an aggregate function: b"