项目作者: araddon

项目描述 :
A golang expression evaluator & Library to build SQL query engine based functionality.
高级语言: Go
项目地址: git://github.com/araddon/qlbridge.git
创建时间: 2014-10-26T01:58:25Z
项目社区:https://github.com/araddon/qlbridge

开源协议:MIT License

下载


QLBridge - Go SQL Runtime Engine

A SQL execution engine for embedded use as a library for SQL or SQL-Like functionality.
Hackable, add datasources (“Storage” can be rest apis, or anything), and add functions. See usage in https://github.com/dataux/dataux
a federated Sql Engine mysql-compatible with backends (Elasticsearch, Google-Datastore, Mongo, Cassandra, Files).

Code Coverage
GoDoc
Build Status
Go ReportCard

QLBridge Features and Goals

  • expression engine for evaluation of single expressions
  • execution of sql queries against your data, embedable, not coupled to storage layer
  • extend VM with custom go functions, provide rich basic library of functions
  • provide example backends (csv, elasticsearch, etc)

Dialects

Example of Expression Evaluation Engine

These expressions can be used stand-alone embedded usage in your app. But,
are the same expressions which might be columns, where, group-by clauses in SQL.
see example

  1. func main() {
  2. // Add a custom function to the VM to make available to expression language
  3. expr.FuncAdd("email_is_valid", &EmailIsValid{})
  4. // This is the evaluation context which will be the data-source
  5. // to be evaluated against the expressions. There is a very simple
  6. // interface you can use to create your own.
  7. evalContext := datasource.NewContextSimpleNative(map[string]interface{}{
  8. "int5": 5,
  9. "str5": "5",
  10. "created": dateparse.MustParse("12/18/2015"),
  11. "bvalt": true,
  12. "bvalf": false,
  13. "user_id": "abc",
  14. "urls": []string{"http://google.com", "http://nytimes.com"},
  15. "hits": map[string]int64{"google.com": 5, "bing.com": 1},
  16. "email": "bob@bob.com",
  17. "emailbad": "bob",
  18. "mt": map[string]time.Time{
  19. "event0": dateparse.MustParse("12/18/2015"),
  20. "event1": dateparse.MustParse("12/22/2015"),
  21. },
  22. })
  23. // Example list of expressions
  24. exprs := []string{
  25. "int5 == 5",
  26. `6 > 5`,
  27. `6 > 5.5`,
  28. `(4 + 5) / 2`,
  29. `6 == (5 + 1)`,
  30. `2 * (3 + 5)`,
  31. `todate("12/12/2012")`,
  32. `created > "now-1M"`, // Date math
  33. `created > "now-10y"`,
  34. `user_id == "abc"`,
  35. `email_is_valid(email)`,
  36. `email_is_valid(emailbad)`,
  37. `email_is_valid("not_an_email")`,
  38. `EXISTS int5`,
  39. `!exists(user_id)`,
  40. `mt.event0 > now()`, // step into child of maps
  41. `["portland"] LIKE "*land"`,
  42. `email contains "bob"`,
  43. `email NOT contains "bob"`,
  44. `[1,2,3] contains int5`,
  45. `[1,2,3,5] NOT contains int5`,
  46. `urls contains "http://google.com"`,
  47. `split("chicago,portland",",") LIKE "*land"`,
  48. `10 BETWEEN 1 AND 50`,
  49. `15.5 BETWEEN 1 AND "55.5"`,
  50. `created BETWEEN "now-50w" AND "12/18/2020"`,
  51. `toint(not_a_field) NOT IN ("a","b" 4.5)`,
  52. `
  53. OR (
  54. email != "bob@bob.com"
  55. AND (
  56. NOT EXISTS not_a_field
  57. int5 == 5
  58. )
  59. )`,
  60. }
  61. for _, expression := range exprs {
  62. // Same ast can be re-used safely concurrently
  63. exprAst := expr.MustParse(expression)
  64. // Evaluate AST in the vm
  65. val, _ := vm.Eval(evalContext, exprAst)
  66. v := val.Value()
  67. u.Debugf("Output: %-35v T:%-15T expr: %s", v, v, expression)
  68. }
  69. }
  70. // Example of a custom Function, that we are making available in the Expression VM
  71. type EmailIsValid struct{}
  72. func (m *EmailIsValid) Validate(n *expr.FuncNode) (expr.EvaluatorFunc, error) {
  73. if len(n.Args) != 1 {
  74. return nil, fmt.Errorf("Expected 1 arg for EmailIsValid(arg) but got %s", n)
  75. }
  76. return func(ctx expr.EvalContext, args []value.Value) (value.Value, bool) {
  77. if args[0] == nil || args[0].Err() || args[0].Nil() {
  78. return value.BoolValueFalse, true
  79. }
  80. if _, err := mail.ParseAddress(args[0].ToString()); err == nil {
  81. return value.BoolValueTrue, true
  82. }
  83. return value.BoolValueFalse, true
  84. }, nil
  85. }
  86. func (m *EmailIsValid) Type() value.ValueType { return value.BoolType }

Example SQL Runtime for Reading a Csv via Stdio, File

See example in qlcsv
folder for a CSV reader, parser, evaluation engine.

  1. ./qlcsv -sql 'select
  2. user_id, email, item_count * 2, yy(reg_date) > 10
  3. FROM stdin where email_is_valid(email);' < users.csv
  1. func main() {
  2. if sqlText == "" {
  3. u.Errorf("You must provide a valid select query in argument: --sql=\"select ...\"")
  4. return
  5. }
  6. // load all of our built-in functions
  7. builtins.LoadAllBuiltins()
  8. // Add a custom function to the VM to make available to SQL language
  9. expr.FuncAdd("email_is_valid", &EmailIsValid{})
  10. // We are registering the "csv" datasource, to show that
  11. // the backend/sources can be easily created/added. This csv
  12. // reader is an example datasource that is very, very simple.
  13. exit := make(chan bool)
  14. src, _ := datasource.NewCsvSource("stdin", 0, bytes.NewReader([]byte("##")), exit)
  15. schema.RegisterSourceAsSchema("example_csv", src)
  16. db, err := sql.Open("qlbridge", "example_csv")
  17. if err != nil {
  18. panic(err.Error())
  19. }
  20. defer db.Close()
  21. rows, err := db.Query(sqlText)
  22. if err != nil {
  23. u.Errorf("could not execute query: %v", err)
  24. return
  25. }
  26. defer rows.Close()
  27. cols, _ := rows.Columns()
  28. // this is just stupid hijinx for getting pointers for unknown len columns
  29. readCols := make([]interface{}, len(cols))
  30. writeCols := make([]string, len(cols))
  31. for i := range writeCols {
  32. readCols[i] = &writeCols[i]
  33. }
  34. fmt.Printf("\n\nScanning through CSV: (%v)\n\n", strings.Join(cols, ","))
  35. for rows.Next() {
  36. rows.Scan(readCols...)
  37. fmt.Println(strings.Join(writeCols, ", "))
  38. }
  39. fmt.Println("")
  40. }
  41. // Example of a custom Function, that we are adding into the Expression VM
  42. //
  43. // select
  44. // user_id AS theuserid, email, item_count * 2, reg_date
  45. // FROM stdin
  46. // WHERE email_is_valid(email)
  47. type EmailIsValid struct{}
  48. func (m *EmailIsValid) Validate(n *expr.FuncNode) (expr.EvaluatorFunc, error) {
  49. if len(n.Args) != 1 {
  50. return nil, fmt.Errorf("Expected 1 arg for EmailIsValid(arg) but got %s", n)
  51. }
  52. return func(ctx expr.EvalContext, args []value.Value) (value.Value, bool) {
  53. if args[0] == nil || args[0].Err() || args[0].Nil() {
  54. return value.BoolValueFalse, true
  55. }
  56. if _, err := mail.ParseAddress(args[0].ToString()); err == nil {
  57. return value.BoolValueTrue, true
  58. }
  59. return value.BoolValueFalse, true
  60. }, nil
  61. }
  62. func (m *EmailIsValid) Type() value.ValueType { return value.BoolType }

[x]QL languages are making a comeback. It is still an easy, approachable
way of working with data. Also, we see more and more ql’s that are xql’ish but
un-apologetically non-standard. This matches our observation that
data is stored in more and more formats in more tools, services that aren’t
traditional db’s but querying that data should still be easy. Examples
Influx,
GitQL,
Presto,
Hive,
CQL,
yql,
ql.io, etc

Projects that access non-sql data via [x]ql

Go Script/VM interpreters