qlbridge

package module
v0.0.0-...-8371fe2 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2016 License: MIT Imports: 1 Imported by: 0

README

QLBridge - a Go SQL Runtime Engine

A SQL execution engine for embedded use as a library for Sql OR sql-Like functionality. Hackable, add datasources, functions. See usage in https://github.com/dataux/dataux a federated Sql Engine mysql-compatible with backends (Elasticsearch, Google-Datastore, Mongo, Cassandra, Files).

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)
Dialects
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
membtree
Membtree implements a Datasource in-memory implemenation using the google btree.
Membtree implements a Datasource in-memory implemenation using the google btree.
memdb
Memdb package implements a Datasource in-memory implemenation using the hashicorp go-memdb (immuteable radix tree's).
Memdb package implements a Datasource in-memory implemenation using the hashicorp go-memdb (immuteable radix tree's).
mockcsv
Mockcsv implements an in-memory csv data source for testing usage implemented by wrapping the mem-b-tree.
Mockcsv implements an in-memory csv data source for testing usage implemented by wrapping the mem-b-tree.
mockcsvtestdata
Mockscsvtestdata is csv test data only used for tests.
Mockscsvtestdata is csv test data only used for tests.
dialects
examples
Exec contains execution tasks to run each of the separate tasks (Source, Project, Where, Having, etc) of a SQL data of tasks.
Exec contains execution tasks to run each of the separate tasks (Source, Project, Where, Having, etc) of a SQL data of tasks.
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 are a library of functions natively available in qlbridge expression evaluation although adding your own is easy.
Builtin functions are a library of functions natively available in qlbridge expression evaluation although adding your own is easy.
Lexing for QLBridge implements 4 Dialects - SQL - FilterQL a Where filtering language - Json - Expression - simple native logical/functional expression evaluator
Lexing for QLBridge implements 4 Dialects - SQL - FilterQL a Where filtering language - Json - Expression - simple native logical/functional expression evaluator
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 }
Package rel are the AST Structures and Parsers for the SQL, FilterQL, and Expression dialects.
Package rel are the AST Structures and Parsers for the SQL, 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