Add DISTINCT operator support
This commit is contained in:
parent
6fe9c26d30
commit
a506a96d6a
7 changed files with 197 additions and 0 deletions
|
|
@ -173,3 +173,8 @@ func (c *caseOperatorImpl) serialize(statement StatementType, out *SQLBuilder, o
|
||||||
|
|
||||||
out.WriteString("END)")
|
out.WriteString("END)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DISTINCT operator can be used to return distinct values of expr
|
||||||
|
func DISTINCT(expr Expression) Expression {
|
||||||
|
return newPrefixOperatorExpression(expr, "DISTINCT")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,3 +7,6 @@ var NOT = jet.NOT
|
||||||
|
|
||||||
// BIT_NOT inverts every bit in integer expression result
|
// BIT_NOT inverts every bit in integer expression result
|
||||||
var BIT_NOT = jet.BIT_NOT
|
var BIT_NOT = jet.BIT_NOT
|
||||||
|
|
||||||
|
// DISTINCT operator can be used to return distinct values of expr
|
||||||
|
var DISTINCT = jet.DISTINCT
|
||||||
|
|
|
||||||
|
|
@ -7,3 +7,6 @@ var NOT = jet.NOT
|
||||||
|
|
||||||
// BIT_NOT inverts every bit in integer expression result
|
// BIT_NOT inverts every bit in integer expression result
|
||||||
var BIT_NOT = jet.BIT_NOT
|
var BIT_NOT = jet.BIT_NOT
|
||||||
|
|
||||||
|
// DISTINCT operator can be used to return distinct values of expr
|
||||||
|
var DISTINCT = jet.DISTINCT
|
||||||
|
|
|
||||||
|
|
@ -7,3 +7,6 @@ var NOT = jet.NOT
|
||||||
|
|
||||||
// BIT_NOT inverts every bit in integer expression result
|
// BIT_NOT inverts every bit in integer expression result
|
||||||
var BIT_NOT = jet.BIT_NOT
|
var BIT_NOT = jet.BIT_NOT
|
||||||
|
|
||||||
|
// DISTINCT operator can be used to return distinct values of expr
|
||||||
|
var DISTINCT = jet.DISTINCT
|
||||||
|
|
|
||||||
|
|
@ -153,6 +153,68 @@ ORDER BY payment.customer_id, SUM(payment.amount) ASC;
|
||||||
requireLogged(t, query)
|
requireLogged(t, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAggregateFunctionDistinct(t *testing.T) {
|
||||||
|
stmt := SELECT(
|
||||||
|
Payment.CustomerID,
|
||||||
|
|
||||||
|
COUNT(DISTINCT(Payment.Amount)).AS("distinct.count"),
|
||||||
|
SUM(DISTINCT(Payment.Amount)).AS("distinct.sum"),
|
||||||
|
AVG(DISTINCT(Payment.Amount)).AS("distinct.avg"),
|
||||||
|
MIN(DISTINCT(Payment.PaymentDate)).AS("distinct.first_payment_date"),
|
||||||
|
MAX(DISTINCT(Payment.PaymentDate)).AS("distinct.last_payment_date"),
|
||||||
|
).FROM(
|
||||||
|
Payment,
|
||||||
|
).WHERE(
|
||||||
|
Payment.CustomerID.EQ(Int(1)),
|
||||||
|
).GROUP_BY(
|
||||||
|
Payment.CustomerID,
|
||||||
|
)
|
||||||
|
|
||||||
|
testutils.AssertDebugStatementSql(t, stmt, `
|
||||||
|
SELECT payment.customer_id AS "payment.customer_id",
|
||||||
|
COUNT(DISTINCT payment.amount) AS "distinct.count",
|
||||||
|
SUM(DISTINCT payment.amount) AS "distinct.sum",
|
||||||
|
AVG(DISTINCT payment.amount) AS "distinct.avg",
|
||||||
|
MIN(DISTINCT payment.payment_date) AS "distinct.first_payment_date",
|
||||||
|
MAX(DISTINCT payment.payment_date) AS "distinct.last_payment_date"
|
||||||
|
FROM dvds.payment
|
||||||
|
WHERE payment.customer_id = 1
|
||||||
|
GROUP BY payment.customer_id;
|
||||||
|
`)
|
||||||
|
|
||||||
|
type Distinct struct {
|
||||||
|
model.Payment
|
||||||
|
|
||||||
|
Count int64
|
||||||
|
Sum float64
|
||||||
|
Avg float64
|
||||||
|
FirstPaymentDate time.Time
|
||||||
|
LastPaymentDate time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
var dest Distinct
|
||||||
|
|
||||||
|
err := stmt.Query(db, &dest)
|
||||||
|
require.NoError(t, err)
|
||||||
|
testutils.AssertJSON(t, dest, `
|
||||||
|
{
|
||||||
|
"PaymentID": 0,
|
||||||
|
"CustomerID": 1,
|
||||||
|
"StaffID": 0,
|
||||||
|
"RentalID": null,
|
||||||
|
"Amount": 0,
|
||||||
|
"PaymentDate": "0001-01-01T00:00:00Z",
|
||||||
|
"LastUpdate": "0001-01-01T00:00:00Z",
|
||||||
|
"Count": 8,
|
||||||
|
"Sum": 38.92,
|
||||||
|
"Avg": 4.865,
|
||||||
|
"FirstPaymentDate": "2005-05-25T11:30:37Z",
|
||||||
|
"LastPaymentDate": "2005-08-22T20:03:46Z"
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestSubQuery(t *testing.T) {
|
func TestSubQuery(t *testing.T) {
|
||||||
|
|
||||||
rRatingFilms := Film.
|
rRatingFilms := Film.
|
||||||
|
|
|
||||||
|
|
@ -1191,6 +1191,66 @@ ORDER BY customer.customer_id, SUM(payment.amount) ASC;
|
||||||
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/customer_payment_sum.json")
|
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/customer_payment_sum.json")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAggregateFunctionDistinct(t *testing.T) {
|
||||||
|
stmt := SELECT(
|
||||||
|
Payment.CustomerID,
|
||||||
|
|
||||||
|
COUNT(DISTINCT(Payment.Amount)).AS("distinct.count"),
|
||||||
|
SUM(DISTINCT(Payment.Amount)).AS("distinct.sum"),
|
||||||
|
AVG(DISTINCT(Payment.Amount)).AS("distinct.avg"),
|
||||||
|
MIN(DISTINCT(Payment.PaymentDate)).AS("distinct.first_payment_date"),
|
||||||
|
MAX(DISTINCT(Payment.PaymentDate)).AS("distinct.last_payment_date"),
|
||||||
|
).FROM(
|
||||||
|
Payment,
|
||||||
|
).WHERE(
|
||||||
|
Payment.CustomerID.EQ(Int(1)),
|
||||||
|
).GROUP_BY(
|
||||||
|
Payment.CustomerID,
|
||||||
|
)
|
||||||
|
|
||||||
|
testutils.AssertDebugStatementSql(t, stmt, `
|
||||||
|
SELECT payment.customer_id AS "payment.customer_id",
|
||||||
|
COUNT(DISTINCT payment.amount) AS "distinct.count",
|
||||||
|
SUM(DISTINCT payment.amount) AS "distinct.sum",
|
||||||
|
AVG(DISTINCT payment.amount) AS "distinct.avg",
|
||||||
|
MIN(DISTINCT payment.payment_date) AS "distinct.first_payment_date",
|
||||||
|
MAX(DISTINCT payment.payment_date) AS "distinct.last_payment_date"
|
||||||
|
FROM dvds.payment
|
||||||
|
WHERE payment.customer_id = 1
|
||||||
|
GROUP BY payment.customer_id;
|
||||||
|
`)
|
||||||
|
|
||||||
|
type Distinct struct {
|
||||||
|
model.Payment
|
||||||
|
|
||||||
|
Count int64
|
||||||
|
Sum float64
|
||||||
|
Avg float64
|
||||||
|
FirstPaymentDate time.Time
|
||||||
|
LastPaymentDate time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
var dest Distinct
|
||||||
|
|
||||||
|
err := stmt.Query(db, &dest)
|
||||||
|
require.NoError(t, err)
|
||||||
|
testutils.AssertJSON(t, dest, `
|
||||||
|
{
|
||||||
|
"PaymentID": 0,
|
||||||
|
"CustomerID": 1,
|
||||||
|
"StaffID": 0,
|
||||||
|
"RentalID": 0,
|
||||||
|
"Amount": 0,
|
||||||
|
"PaymentDate": "0001-01-01T00:00:00Z",
|
||||||
|
"Count": 8,
|
||||||
|
"Sum": 38.92,
|
||||||
|
"Avg": 4.865,
|
||||||
|
"FirstPaymentDate": "2007-02-14T23:22:38.996577Z",
|
||||||
|
"LastPaymentDate": "2007-04-30T01:10:44.996577Z"
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSelectGroupBy2(t *testing.T) {
|
func TestSelectGroupBy2(t *testing.T) {
|
||||||
expectedSQL := `
|
expectedSQL := `
|
||||||
SELECT customer.customer_id AS "customer.customer_id",
|
SELECT customer.customer_id AS "customer.customer_id",
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,67 @@ ORDER BY payment.customer_id, SUM(payment.amount) ASC;
|
||||||
requireLogged(t, query)
|
requireLogged(t, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAggregateFunctionDistinct(t *testing.T) {
|
||||||
|
stmt := SELECT(
|
||||||
|
Payment.CustomerID,
|
||||||
|
|
||||||
|
COUNT(DISTINCT(Payment.Amount)).AS("distinct.count"),
|
||||||
|
SUM(DISTINCT(Payment.Amount)).AS("distinct.sum"),
|
||||||
|
AVG(DISTINCT(Payment.Amount)).AS("distinct.avg"),
|
||||||
|
MIN(DISTINCT(Payment.PaymentDate)).AS("distinct.first_payment_date"),
|
||||||
|
MAX(DISTINCT(Payment.PaymentDate)).AS("distinct.last_payment_date"),
|
||||||
|
).FROM(
|
||||||
|
Payment,
|
||||||
|
).WHERE(
|
||||||
|
Payment.CustomerID.EQ(Int(1)),
|
||||||
|
).GROUP_BY(
|
||||||
|
Payment.CustomerID,
|
||||||
|
)
|
||||||
|
|
||||||
|
testutils.AssertDebugStatementSql(t, stmt, `
|
||||||
|
SELECT payment.customer_id AS "payment.customer_id",
|
||||||
|
COUNT(DISTINCT payment.amount) AS "distinct.count",
|
||||||
|
SUM(DISTINCT payment.amount) AS "distinct.sum",
|
||||||
|
AVG(DISTINCT payment.amount) AS "distinct.avg",
|
||||||
|
MIN(DISTINCT payment.payment_date) AS "distinct.first_payment_date",
|
||||||
|
MAX(DISTINCT payment.payment_date) AS "distinct.last_payment_date"
|
||||||
|
FROM payment
|
||||||
|
WHERE payment.customer_id = 1
|
||||||
|
GROUP BY payment.customer_id;
|
||||||
|
`)
|
||||||
|
|
||||||
|
type Distinct struct {
|
||||||
|
model.Payment
|
||||||
|
|
||||||
|
Count int64
|
||||||
|
Sum float64
|
||||||
|
Avg float64
|
||||||
|
FirstPaymentDate time.Time
|
||||||
|
LastPaymentDate time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
var dest Distinct
|
||||||
|
|
||||||
|
err := stmt.Query(db, &dest)
|
||||||
|
require.NoError(t, err)
|
||||||
|
testutils.AssertJSON(t, dest, `
|
||||||
|
{
|
||||||
|
"PaymentID": 0,
|
||||||
|
"CustomerID": 1,
|
||||||
|
"StaffID": 0,
|
||||||
|
"RentalID": null,
|
||||||
|
"Amount": 0,
|
||||||
|
"PaymentDate": "0001-01-01T00:00:00Z",
|
||||||
|
"LastUpdate": "0001-01-01T00:00:00Z",
|
||||||
|
"Count": 8,
|
||||||
|
"Sum": 38.92000000000001,
|
||||||
|
"Avg": 4.865000000000001,
|
||||||
|
"FirstPaymentDate": "2005-05-25T11:30:37Z",
|
||||||
|
"LastPaymentDate": "2005-08-22T20:03:46Z"
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSubQuery(t *testing.T) {
|
func TestSubQuery(t *testing.T) {
|
||||||
|
|
||||||
rRatingFilms :=
|
rRatingFilms :=
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue