Skip to content

Instantly share code, notes, and snippets.

@ligustah

ligustah/main.go Secret

Created January 10, 2021 16:01
Show Gist options
  • Save ligustah/2f058e3dc6635bd113048301d1c227e7 to your computer and use it in GitHub Desktop.
Save ligustah/2f058e3dc6635bd113048301d1c227e7 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/parse"
)
const (
broken = `
select *
from contract c
left join contracting_party cp on cp.id = c.counter_party_id
left join contract_activities ca on ca.contract_id = c.id
left join activity a on ca.activities_id = a.id
left join comment_activity cac on cac.id = a.id
left join document_update_activity dac on dac.id = a.id
left join document_revision dr on dr.id = dac.document_revision_id
`
working = `
select *
from contract c
left join contracting_party cp on cp.id = c.counter_party_id
left join contract_activities ca on ca.contract_id = c.id
left join activity a on ca.activities_id = a.id
left join comment_activity cac on cac.id = a.id
-- left join document_update_activity dac on dac.id = a.id
-- left join document_revision dr on dr.id = dac.document_revision_id
`
)
func runQuery(driver *sqle.Engine, query string) {
ctx := sql.NewContext(context.Background())
ctx.SetCurrentDatabase("test")
plan, err := parse.Parse(ctx, query)
if err != nil {
log.Fatal(err, plan)
}
log.Print(plan.String())
analyzed, err := driver.Analyzer.Analyze(ctx, plan, nil)
if err != nil {
log.Fatal(err, analyzed)
}
log.Print(analyzed.String())
}
func main() {
db := memory.NewDatabase("test")
tables := []sql.Table{
memory.NewTable("contract", sql.Schema{
{Name: "id", Type: sql.Int64, Source: "contract", PrimaryKey: true},
{Name: "counter_party_id", Type: sql.Int64, Source: "contract"},
}),
memory.NewTable("activity", sql.Schema{
{Name: "id", Type: sql.Int64, Source: "activity", PrimaryKey: true},
}),
memory.NewTable("contract_activities", sql.Schema{
{Name: "contract_id", Type: sql.Int64, Source: "contract_activities"},
{Name: "activities_id", Type: sql.Int64, Source: "contract_activities"},
}),
memory.NewTable("contracting_party", sql.Schema{
{Name: "id", Type: sql.Int64, Source: "contracting_party", PrimaryKey: true},
}),
memory.NewTable("comment_activity", sql.Schema{
{Name: "id", Type: sql.Int64, Source: "comment_activity", PrimaryKey: true},
}),
memory.NewTable("contract_document_revisions", sql.Schema{
{Name: "contract_id", Type: sql.Int64, Source: "contract_document_revisions"},
{Name: "document_revisions_id", Type: sql.Int64, Source: "contract_document_revisions"},
}),
memory.NewTable("document_revision", sql.Schema{
{Name: "id", Type: sql.Int64, Source: "document_revision", PrimaryKey: true},
}),
memory.NewTable("document_update_activity", sql.Schema{
{Name: "id", Type: sql.Int64, Source: "document_update_activity", PrimaryKey: true},
{Name: "document_revision_id", Type: sql.Int64, Source: "document_update_activity"},
}),
}
for _, table := range tables {
db.AddTable(table.Name(), table)
}
driver := sqle.NewDefault()
driver.AddDatabase(db)
log.Println("Working query")
runQuery(driver, working)
log.Println("Broken query")
runQuery(driver, broken)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment