qlbridge

package module
v0.0.0-...-261b5b0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2016 License: MIT Imports: 1 Imported by: 0

README

QLBridge - a Go SQL Runtime Engine

This is a SQL execution engine to process data with sql for embedded use, includes a native go lexer, parser. Extend with native go functions. Intended as a library to help build SQL or similar expression functionality into your application. See https://github.com/dataux/dataux for an example.

QLBridge Features and Goals
  • 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)
Example SQL Runtime for Reading a Csv via Stdio, File

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


./qlcsv -sql 'select 
		user_id, email, item_count * 2, yy(reg_date) > 10 
	FROM stdio where email_is_valid(email);' < users.csv


func main() {

	// load the libray of pre-built functions for usage in sql queries
	builtins.LoadAllBuiltins()

	// Add a custom function to the VM to make available to SQL language
	// showing lexer/parser accepts it
	expr.FuncAdd("email_is_valid", EmailIsValid)

	// Datasources are easy to write and can be added
	datasource.Register("csv", &datasource.CsvDataSource{})

	// now from here down is standard go database/sql query handling
	db, err := sql.Open("qlbridge", "csv:///dev/stdin")
	if err != nil {
		panic(err.Error())
	}
	defer db.Close()

	rows, err := db.Query(sqlText)
	if err != nil {
		panic(err.Error())
	}
	defer rows.Close()
	cols, _ := rows.Columns()

	// this is just stupid hijinx for getting pointers for unknown len columns
	readCols := make([]interface{}, len(cols))
	writeCols := make([]string, len(cols))
	for i, _ := range writeCols {
		readCols[i] = &writeCols[i]
	}

	for rows.Next() {
		rows.Scan(readCols...)
		fmt.Println(strings.Join(writeCols, ", "))
	}
}

// Example of a custom Function, that we are adding into the Expression VM
//
//         select
//              user_id AS theuserid, email, item_count * 2, reg_date
//         FROM stdio
//         WHERE email_is_valid(email)
func EmailIsValid(ctx vm.EvalContext, email value.Value) (value.BoolValue, bool) {
	emailstr, ok := value.ToString(email.Rv())
	if !ok || emailstr == "" {
		return value.BoolValueFalse, true
	}
	if _, err := mail.ParseAddress(emailstr); err == nil {
		return value.BoolValueTrue, true
	}

	return value.BoolValueFalse, true
}


[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

Documentation

Overview

QLBridge is a SQL Relational algebra and expression package for embedding sql like functionality into your app. Includes Lexer, Parsers, different SQL Dialects, as well as planners and executors.

Directories

Path Synopsis
dialects
examples
Exececution tasks and executor for DAG of plan tasks can be embedded and used, or extended using Executor interface.
Exececution tasks and executor for DAG of plan tasks can be embedded and used, or extended using Executor interface.
Expression structures, ie the `a = b` type expression syntax including parser, node types, boolean logic check, functions.
Expression structures, ie the `a = b` type expression syntax including parser, node types, boolean logic check, functions.
builtins
Builtin functions injected into expression vm.
Builtin functions injected into expression vm.
Lexing for QLBridge
Lexing for QLBridge
Plan structures, converts AST into a plan, with a DAG of tasks that comprise that plan, the planner is pluggable.
Plan structures, converts AST into a plan, with a DAG of tasks that comprise that plan, the planner is pluggable.
Package driver registers a QL Bridge sql/driver named "qlbridge" Usage package main import ( "database/sql" _ "github.com/araddon/qlbridge/qlbdriver" ) func main() { db, err := sql.Open("qlbridge", "csv:///dev/stdin") if err != nil { log.Fatal(err) } // Use db here }
Package driver registers a QL Bridge sql/driver named "qlbridge" Usage package main import ( "database/sql" _ "github.com/araddon/qlbridge/qlbdriver" ) func main() { db, err := sql.Open("qlbridge", "csv:///dev/stdin") if err != nil { log.Fatal(err) } // Use db here }
AST Structures and Parsers for the SQL, and FilterQL, and Expression dialects.
AST Structures and Parsers for the SQL, and FilterQL, and Expression dialects.
The core Relational Algrebra schema objects such as Table, Schema, DataSource, Fields, Headers, Index.
The core Relational Algrebra schema objects such as Table, Schema, DataSource, Fields, Headers, Index.
Test only package for harness to load, implement SQL tests
Test only package for harness to load, implement SQL tests
The core value types (string, int, etc) for the virtual machine, parsing etc.
The core value types (string, int, etc) for the virtual machine, parsing etc.
vm
VM implements the virtual machine runtime/evaluator for the SQL, FilterQL, and Expression evalutors.
VM implements the virtual machine runtime/evaluator for the SQL, FilterQL, and Expression evalutors.
_bm

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL