Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion module/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (t *ClickHouseTable) Destroy() error {
// To find the method, we will ask the database to explain the query and return the best method
func (t *ClickHouseTable) BestIndex(cst []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy, info sqlite3.IndexInformation) (*sqlite3.IndexResult, error) {
// Create the SQL query
queryBuilder, limitCstIndex, offsetCstIndex, used := efficientConstructSQLQuery(cst, ob, t.schema, t.tableName, info.ColUsed)
queryBuilder, limitCstIndex, offsetCstIndex, used := efficientConstructSQLQuery(cst, ob, t.schema, t.tableName, info.ColUsed, sqlbuilder.ClickHouse)
queryBuilder.SetFlavor(sqlbuilder.ClickHouse)
rawQuery, args := queryBuilder.Build()
rawQuery += sqlQuerySuffix
Expand Down
22 changes: 15 additions & 7 deletions module/db_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ func constructSQLQuery(
ob []sqlite3.InfoOrderBy,
columns []databaseColumn,
table string,
flavor sqlbuilder.Flavor,

) (query *sqlbuilder.SelectBuilder, limit int, offset int, used []bool) {

// Initialize the SQL query builder
query = sqlbuilder.NewSelectBuilder()
query.SetFlavor(flavor)
// Add all the columns to the query
cols := []string{}
for _, col := range columns {
cols = append(cols, col.Realname)
// Quote column names to handle reserved keywords (e.g., `order` for MySQL)
cols = append(cols, flavor.Quote(col.Realname))
}

query.Select(cols...).From(table)
Expand Down Expand Up @@ -112,10 +115,11 @@ func constructSQLQuery(

// Add the order by
for _, o := range ob {
quotedCol := flavor.Quote(columns[o.Column].Realname)
if o.Desc {
query.OrderBy(columns[o.Column].Realname + " DESC")
query.OrderBy(quotedCol + " DESC")
} else {
query.OrderBy(columns[o.Column].Realname + " ASC")
query.OrderBy(quotedCol + " ASC")
}
}

Expand All @@ -129,27 +133,30 @@ func efficientConstructSQLQuery(
columns []databaseColumn,
table string,
colUsed uint64,
flavor sqlbuilder.Flavor,

) (query *sqlbuilder.SelectBuilder, limit int, offset int, used []bool) {

// Initialize the SQL query builder
query = sqlbuilder.NewSelectBuilder()
query.SetFlavor(flavor)
// Add all the columns to the query
cols := []string{}
for i, col := range columns {
if colUsed&(1<<i) == 0 && i < 62 { // colUsed is a bitmask, and the 63rd bit is reserved to say, it means more columns are used
// If the column is not used, we skip it
continue
}
cols = append(cols, col.Realname)
// Quote column names to handle reserved keywords (e.g., `order` for MySQL)
cols = append(cols, flavor.Quote(col.Realname))
}

// If no columns are used, we add the first one
//
// When SQLite does a SELECT count(*), it doesn't use any column, so we need to add at least one column
// because most SQL engines require at least one column in the SELECT clause.
if len(cols) == 0 {
cols = append(cols, columns[0].Realname)
cols = append(cols, flavor.Quote(columns[0].Realname))
}

query.Select(cols...).From(table)
Expand Down Expand Up @@ -223,10 +230,11 @@ func efficientConstructSQLQuery(

// Add the order by
for _, o := range ob {
quotedCol := flavor.Quote(columns[o.Column].Realname)
if o.Desc {
query.OrderBy(columns[o.Column].Realname + " DESC")
query.OrderBy(quotedCol + " DESC")
} else {
query.OrderBy(columns[o.Column].Realname + " ASC")
query.OrderBy(quotedCol + " ASC")
}
}

Expand Down
2 changes: 1 addition & 1 deletion module/duckdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (t *DuckDBTable) Destroy() error {
// To find the method, we will ask the database to explain the query and return the best method
func (t *DuckDBTable) BestIndex(cst []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy, info sqlite3.IndexInformation) (*sqlite3.IndexResult, error) {
// Create the SQL query
queryBuilder, limitCstIndex, offsetCstIndex, used := efficientConstructSQLQuery(cst, ob, t.schema, t.tableName, info.ColUsed)
queryBuilder, limitCstIndex, offsetCstIndex, used := efficientConstructSQLQuery(cst, ob, t.schema, t.tableName, info.ColUsed, sqlbuilder.PostgreSQL)
queryBuilder.SetFlavor(sqlbuilder.PostgreSQL)
rawQuery, args := queryBuilder.Build()

Expand Down
2 changes: 1 addition & 1 deletion module/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func (t *MySQLTable) Destroy() error {
// To find the method, we will ask the database to explain the query and return the best method
func (t *MySQLTable) BestIndex(cst []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy, info sqlite3.IndexInformation) (*sqlite3.IndexResult, error) {
// Create the SQL query
queryBuilder, limitCstIndex, offsetCstIndex, used := constructSQLQuery(cst, ob, t.schema, t.tableName)
queryBuilder, limitCstIndex, offsetCstIndex, used := constructSQLQuery(cst, ob, t.schema, t.tableName, sqlbuilder.MySQL)
queryBuilder.SetFlavor(sqlbuilder.MySQL)
rawQuery, args := queryBuilder.Build()
rawQuery += sqlQuerySuffix
Expand Down
2 changes: 1 addition & 1 deletion module/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (t *PostgresTable) Destroy() error {
// To find the method, we will ask the database to explain the query and return the best method
func (t *PostgresTable) BestIndex(cst []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy, info sqlite3.IndexInformation) (*sqlite3.IndexResult, error) {
// Create the SQL query
queryBuilder, limitCstIndex, offsetCstIndex, used := constructSQLQuery(cst, ob, t.schema, t.tableName)
queryBuilder, limitCstIndex, offsetCstIndex, used := constructSQLQuery(cst, ob, t.schema, t.tableName, sqlbuilder.PostgreSQL)
queryBuilder.SetFlavor(sqlbuilder.PostgreSQL)
rawQuery, args := queryBuilder.Build()
rawQuery += sqlQuerySuffix
Expand Down