2019-07-30 11:45:10 +02:00
|
|
|
package postgres
|
2019-07-17 11:03:16 +02:00
|
|
|
|
|
|
|
|
import (
|
2025-03-11 13:33:39 +01:00
|
|
|
"github.com/bytedance/sonic"
|
2026-05-14 16:26:47 +00:00
|
|
|
"source.gleipnir.technology/Gleipnir/jet/internal/testutils"
|
|
|
|
|
. "source.gleipnir.technology/Gleipnir/jet/postgres"
|
|
|
|
|
"source.gleipnir.technology/Gleipnir/jet/tests/.gentestdata/jetdb/northwind/model"
|
|
|
|
|
. "source.gleipnir.technology/Gleipnir/jet/tests/.gentestdata/jetdb/northwind/table"
|
2020-05-09 11:00:22 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-07-17 11:03:16 +02:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
type Dest []struct {
|
|
|
|
|
model.Customers
|
|
|
|
|
|
|
|
|
|
Demographics model.CustomerDemographics
|
|
|
|
|
|
|
|
|
|
Orders []struct {
|
|
|
|
|
model.Orders
|
|
|
|
|
|
|
|
|
|
Shipper model.Shippers
|
|
|
|
|
|
|
|
|
|
Employee struct {
|
|
|
|
|
model.Employees
|
|
|
|
|
|
|
|
|
|
Territories []struct {
|
|
|
|
|
model.Territories
|
|
|
|
|
|
|
|
|
|
Region model.Region
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Details []struct {
|
|
|
|
|
model.OrderDetails
|
|
|
|
|
|
|
|
|
|
Products struct {
|
|
|
|
|
model.Products
|
|
|
|
|
|
|
|
|
|
Category model.Categories
|
|
|
|
|
Supplier model.Suppliers
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkTestNorthwindJoinEverything(b *testing.B) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
testNorthwindJoinEverything(b)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTestNorthwindJoinEverything(t *testing.T) {
|
|
|
|
|
testNorthwindJoinEverything(t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testNorthwindJoinEverything(t require.TestingT) {
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2024-10-17 14:12:21 +02:00
|
|
|
stmt :=
|
2019-07-17 11:03:16 +02:00
|
|
|
SELECT(
|
|
|
|
|
Customers.AllColumns,
|
|
|
|
|
CustomerDemographics.AllColumns,
|
|
|
|
|
Orders.AllColumns,
|
|
|
|
|
Shippers.AllColumns,
|
|
|
|
|
OrderDetails.AllColumns,
|
|
|
|
|
Products.AllColumns,
|
|
|
|
|
Categories.AllColumns,
|
|
|
|
|
Suppliers.AllColumns,
|
2025-02-21 19:55:01 +01:00
|
|
|
Employees.AllColumns,
|
|
|
|
|
Territories.AllColumns,
|
|
|
|
|
Region.AllColumns,
|
2024-10-17 14:12:21 +02:00
|
|
|
).FROM(
|
|
|
|
|
Customers.
|
|
|
|
|
LEFT_JOIN(CustomerCustomerDemo, Customers.CustomerID.EQ(CustomerCustomerDemo.CustomerID)).
|
|
|
|
|
LEFT_JOIN(CustomerDemographics, CustomerCustomerDemo.CustomerTypeID.EQ(CustomerDemographics.CustomerTypeID)).
|
|
|
|
|
LEFT_JOIN(Orders, Orders.CustomerID.EQ(Customers.CustomerID)).
|
|
|
|
|
LEFT_JOIN(Shippers, Orders.ShipVia.EQ(Shippers.ShipperID)).
|
|
|
|
|
LEFT_JOIN(OrderDetails, Orders.OrderID.EQ(OrderDetails.OrderID)).
|
|
|
|
|
LEFT_JOIN(Products, OrderDetails.ProductID.EQ(Products.ProductID)).
|
|
|
|
|
LEFT_JOIN(Categories, Products.CategoryID.EQ(Categories.CategoryID)).
|
|
|
|
|
LEFT_JOIN(Suppliers, Products.SupplierID.EQ(Suppliers.SupplierID)).
|
|
|
|
|
LEFT_JOIN(Employees, Orders.EmployeeID.EQ(Employees.EmployeeID)).
|
|
|
|
|
LEFT_JOIN(EmployeeTerritories, EmployeeTerritories.EmployeeID.EQ(Employees.EmployeeID)).
|
|
|
|
|
LEFT_JOIN(Territories, EmployeeTerritories.TerritoryID.EQ(Territories.TerritoryID)).
|
|
|
|
|
LEFT_JOIN(Region, Territories.RegionID.EQ(Region.RegionID)),
|
2025-02-21 19:55:01 +01:00
|
|
|
).ORDER_BY(
|
|
|
|
|
Customers.CustomerID,
|
|
|
|
|
Orders.OrderID,
|
|
|
|
|
Products.ProductID,
|
|
|
|
|
Territories.TerritoryID,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//fmt.Println(stmt.DebugSql())
|
|
|
|
|
|
|
|
|
|
var dest Dest
|
|
|
|
|
|
|
|
|
|
err := stmt.Query(db, &dest)
|
|
|
|
|
require.NoError(t, err)
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
//testutils.SaveJSONFile(dest, "./testdata/results/postgres/northwind-all.json")
|
|
|
|
|
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/northwind-all.json")
|
|
|
|
|
requireLogged(t, stmt)
|
|
|
|
|
}
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func BenchmarkTestNorthwindJoinEverythingJson(b *testing.B) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
testNorthwindJoinEverythingJson(b)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func TestNorthwindJoinEverythingJson(t *testing.T) {
|
|
|
|
|
testNorthwindJoinEverythingJson(t)
|
|
|
|
|
}
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2025-03-11 13:33:39 +01:00
|
|
|
func BenchmarkTestNorthwindJoinEverythingSonicJson(b *testing.B) {
|
|
|
|
|
useJsonUnmarshalFunc(sonic.Unmarshal, func() {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
testNorthwindJoinEverythingJson(b)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-31 20:04:31 +02:00
|
|
|
func TestNorthwindJoinEverythingJsonSonic(t *testing.T) {
|
|
|
|
|
useJsonUnmarshalFunc(sonic.Unmarshal, func() {
|
|
|
|
|
testNorthwindJoinEverythingJson(t)
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-03-11 13:33:39 +01:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func testNorthwindJoinEverythingJson(t require.TestingT) {
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
stmt := SELECT_JSON_ARR(
|
|
|
|
|
Customers.AllColumns,
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
SELECT_JSON_OBJ(CustomerDemographics.AllColumns).
|
|
|
|
|
FROM(CustomerDemographics.INNER_JOIN(CustomerCustomerDemo, CustomerCustomerDemo.CustomerTypeID.EQ(CustomerDemographics.CustomerTypeID))).
|
|
|
|
|
WHERE(CustomerCustomerDemo.CustomerID.EQ(Customers.CustomerID)).AS("Demographics"),
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
SELECT_JSON_ARR(
|
|
|
|
|
Orders.AllColumns,
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
SELECT_JSON_OBJ(Shippers.AllColumns).
|
|
|
|
|
FROM(Shippers).
|
|
|
|
|
WHERE(Shippers.ShipperID.EQ(Orders.ShipVia)).AS("Shipper"),
|
|
|
|
|
|
|
|
|
|
SELECT_JSON_OBJ(
|
|
|
|
|
Employees.AllColumns,
|
|
|
|
|
SELECT_JSON_ARR(
|
|
|
|
|
Territories.AllColumns,
|
|
|
|
|
|
|
|
|
|
SELECT_JSON_OBJ(Region.AllColumns).
|
|
|
|
|
FROM(Region).
|
|
|
|
|
WHERE(Region.RegionID.EQ(Territories.RegionID)).AS("Region"),
|
|
|
|
|
).FROM(
|
|
|
|
|
EmployeeTerritories.LEFT_JOIN(
|
|
|
|
|
Territories,
|
|
|
|
|
EmployeeTerritories.TerritoryID.EQ(Territories.TerritoryID)),
|
|
|
|
|
).WHERE(
|
2025-03-08 19:01:37 +01:00
|
|
|
EmployeeTerritories.EmployeeID.EQ(Employees.EmployeeID),
|
2025-02-21 19:55:01 +01:00
|
|
|
).AS("Territories"),
|
|
|
|
|
).FROM(Employees).
|
|
|
|
|
WHERE(Orders.EmployeeID.EQ(Employees.EmployeeID)).AS("Employee"),
|
|
|
|
|
|
|
|
|
|
SELECT_JSON_ARR(
|
|
|
|
|
OrderDetails.AllColumns,
|
|
|
|
|
|
|
|
|
|
SELECT_JSON_OBJ(
|
|
|
|
|
Products.AllColumns,
|
|
|
|
|
|
|
|
|
|
SELECT_JSON_OBJ(
|
|
|
|
|
Categories.AllColumns,
|
|
|
|
|
).FROM(Categories).
|
|
|
|
|
WHERE(Categories.CategoryID.EQ(Products.CategoryID)).AS("Category"),
|
|
|
|
|
|
|
|
|
|
SELECT_JSON_OBJ(Suppliers.AllColumns).
|
|
|
|
|
FROM(Suppliers).
|
|
|
|
|
WHERE(Suppliers.SupplierID.EQ(Products.SupplierID)).AS("Supplier"),
|
|
|
|
|
).FROM(Products).
|
|
|
|
|
WHERE(Products.ProductID.EQ(OrderDetails.ProductID)).AS("Products"),
|
|
|
|
|
).FROM(
|
|
|
|
|
OrderDetails,
|
|
|
|
|
).WHERE(
|
|
|
|
|
OrderDetails.OrderID.EQ(Orders.OrderID),
|
|
|
|
|
).AS("Details"),
|
|
|
|
|
).FROM(
|
|
|
|
|
Orders,
|
|
|
|
|
).WHERE(
|
|
|
|
|
Orders.CustomerID.EQ(Customers.CustomerID),
|
|
|
|
|
).ORDER_BY(
|
|
|
|
|
Orders.OrderID,
|
|
|
|
|
).AS("Orders"),
|
|
|
|
|
).FROM(
|
|
|
|
|
Customers,
|
|
|
|
|
).ORDER_BY(
|
|
|
|
|
Customers.CustomerID,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//fmt.Println(stmt.DebugSql())
|
|
|
|
|
|
|
|
|
|
var dest Dest
|
|
|
|
|
|
2025-03-09 17:46:34 +01:00
|
|
|
err := stmt.QueryContext(ctx, db, &dest)
|
2020-05-09 11:00:22 +02:00
|
|
|
require.NoError(t, err)
|
2019-07-17 11:03:16 +02:00
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
//testutils.SaveJSONFile(dest, "./testdata/results/postgres/northwind-all2.json")
|
2019-08-16 12:43:41 +02:00
|
|
|
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/northwind-all.json")
|
2019-07-17 11:03:16 +02:00
|
|
|
}
|
2025-03-13 12:45:01 +01:00
|
|
|
|
|
|
|
|
func TestColumnListFrom(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
toOrderBy := map[string]bool{
|
|
|
|
|
"city": true,
|
|
|
|
|
"region": true,
|
|
|
|
|
"postal_code": true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subQuery := SELECT(
|
|
|
|
|
Customers.AllColumns,
|
|
|
|
|
).FROM(
|
|
|
|
|
Customers,
|
|
|
|
|
).AsTable("subQuery")
|
|
|
|
|
|
|
|
|
|
var orderBy []OrderByClause
|
|
|
|
|
|
|
|
|
|
for _, column := range Customers.AllColumns.From(subQuery) {
|
|
|
|
|
if toOrderBy[column.Name()] {
|
|
|
|
|
orderBy = append(orderBy, column.ASC())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt := SELECT(
|
|
|
|
|
subQuery.AllColumns(),
|
|
|
|
|
).FROM(
|
|
|
|
|
subQuery,
|
|
|
|
|
).ORDER_BY(
|
|
|
|
|
orderBy...,
|
|
|
|
|
).LIMIT(3)
|
|
|
|
|
|
|
|
|
|
testutils.AssertDebugStatementSql(t, stmt, `
|
|
|
|
|
SELECT "subQuery"."customers.customer_id" AS "customers.customer_id",
|
|
|
|
|
"subQuery"."customers.company_name" AS "customers.company_name",
|
|
|
|
|
"subQuery"."customers.contact_name" AS "customers.contact_name",
|
|
|
|
|
"subQuery"."customers.contact_title" AS "customers.contact_title",
|
|
|
|
|
"subQuery"."customers.address" AS "customers.address",
|
|
|
|
|
"subQuery"."customers.city" AS "customers.city",
|
|
|
|
|
"subQuery"."customers.region" AS "customers.region",
|
|
|
|
|
"subQuery"."customers.postal_code" AS "customers.postal_code",
|
|
|
|
|
"subQuery"."customers.country" AS "customers.country",
|
|
|
|
|
"subQuery"."customers.phone" AS "customers.phone",
|
|
|
|
|
"subQuery"."customers.fax" AS "customers.fax"
|
|
|
|
|
FROM (
|
|
|
|
|
SELECT customers.customer_id AS "customers.customer_id",
|
|
|
|
|
customers.company_name AS "customers.company_name",
|
|
|
|
|
customers.contact_name AS "customers.contact_name",
|
|
|
|
|
customers.contact_title AS "customers.contact_title",
|
|
|
|
|
customers.address AS "customers.address",
|
|
|
|
|
customers.city AS "customers.city",
|
|
|
|
|
customers.region AS "customers.region",
|
|
|
|
|
customers.postal_code AS "customers.postal_code",
|
|
|
|
|
customers.country AS "customers.country",
|
|
|
|
|
customers.phone AS "customers.phone",
|
|
|
|
|
customers.fax AS "customers.fax"
|
|
|
|
|
FROM northwind.customers
|
|
|
|
|
) AS "subQuery"
|
|
|
|
|
ORDER BY "subQuery"."customers.city" ASC, "subQuery"."customers.region" ASC, "subQuery"."customers.postal_code" ASC
|
|
|
|
|
LIMIT 3;
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
|
|
var dest []model.Customers
|
|
|
|
|
|
|
|
|
|
err := stmt.Query(db, &dest)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
testutils.AssertJSON(t, dest, `
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"CustomerID": "DRACD",
|
|
|
|
|
"CompanyName": "Drachenblut Delikatessen",
|
|
|
|
|
"ContactName": "Sven Ottlieb",
|
|
|
|
|
"ContactTitle": "Order Administrator",
|
|
|
|
|
"Address": "Walserweg 21",
|
|
|
|
|
"City": "Aachen",
|
|
|
|
|
"Region": null,
|
|
|
|
|
"PostalCode": "52066",
|
|
|
|
|
"Country": "Germany",
|
|
|
|
|
"Phone": "0241-039123",
|
|
|
|
|
"Fax": "0241-059428"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"CustomerID": "RATTC",
|
|
|
|
|
"CompanyName": "Rattlesnake Canyon Grocery",
|
|
|
|
|
"ContactName": "Paula Wilson",
|
|
|
|
|
"ContactTitle": "Assistant Sales Representative",
|
|
|
|
|
"Address": "2817 Milton Dr.",
|
|
|
|
|
"City": "Albuquerque",
|
|
|
|
|
"Region": "NM",
|
|
|
|
|
"PostalCode": "87110",
|
|
|
|
|
"Country": "USA",
|
|
|
|
|
"Phone": "(505) 555-5939",
|
|
|
|
|
"Fax": "(505) 555-3620"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"CustomerID": "OLDWO",
|
|
|
|
|
"CompanyName": "Old World Delicatessen",
|
|
|
|
|
"ContactName": "Rene Phillips",
|
|
|
|
|
"ContactTitle": "Sales Representative",
|
|
|
|
|
"Address": "2743 Bering St.",
|
|
|
|
|
"City": "Anchorage",
|
|
|
|
|
"Region": "AK",
|
|
|
|
|
"PostalCode": "99508",
|
|
|
|
|
"Country": "USA",
|
|
|
|
|
"Phone": "(907) 555-7584",
|
|
|
|
|
"Fax": "(907) 555-2880"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
|
|
}
|