Git Product home page Git Product logo

rgviz's Introduction

Rgviz

Build Status

This library implements the query language for the Google Visualization API wire protocol.

It can generate an AST of the query from a string. The AST nodes implement the Visitor Pattern so you can easily work with it.

Installation

gem install rgviz

Ruby on Rails

There is a separate project on top of this library called rgviz-rails, check it out!

Usage

First you must require the library:

require 'rubygems'
require 'rgviz'

To parse a query:

query = Rgviz::Parser.parse 'select name where age > 20'

More documentation

Read the source code for the AST nodes, it's not very big and the nodes just map to the query structure.

Extensions

Rgviz supports the following extra functions:

  • concat: converts each of its arguments to a string and then concatenates them. For example: concat(1, '-', '2') returns '1-2'. Can also receive just a single argument to convert it to a string.
  • floor
  • round

These new functions are not part of Google's query language, but they are very handy so we added them. These functions are also supported by rgviz-rails.

Using the Visitor Pattern

class MyVisitor < Rgviz::Visitor
  def visit_select(node)
    # do something with the node
    puts 'before select'
    puts node.columns.length

    # returning true means visiting this node children
    true
  end

  def end_visit_select(node)
    # This will be invoked after visiting the node
    puts "after select"
  end

  def visit_id_column(node)
    puts node.name
  end
end

query = Rgviz::Parser.parse 'select name, age'
query.accept MyVisitor.new

# outputs:
# before select
# 2
# name
# age
# after select

There is a visit_XXX and end_visit_XXX for every node in the language.

Wrappers for Google DataTable and others

Their source code is here. You can use them to generate the javascript code to implement the wire protocol.

Output Formatters

You can use Rgviz::HtmlRenderer.render(table) and Rgviz::CsvRenderer.render(table) to get a string to render in html or csv output format.

Contributors

rgviz's People

Contributors

juanboca avatar tippettssh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rgviz's Issues

Sum if query

Is it posisble to a make a query that do a sum with if inside?

For example :

SUM(if( users.origin IS NULL, 1,0))

Checked the documentation but don't find the if statement, there is a workaround?

"where column is null" is case sensitive

The is null constraint is case sensitive. When it is all uppercase, I receive an invalid query. Seems to only work with all lower case. Not sure if this is a bug as I am fine doing all lower case, but it was at least unexpected behavior for me.

Time.zone is not respected when parsing dates

When I submit a query such as SELECT * WHERE created_at >= datetime 'March 08, 2012 03:00:00' I would expect the parsing of that timestamp to respect the time zone set in Time.zone.

I tried to fix this first in rgviz-rails Rgviz::Executor#column_value before realizing this was the wrong method :( The method I am looking for is Rgviz::Parser#parse_atomic_column but I am not sure how to fix it. Time.zone seems to be a rails enhancement and rgviz is supposed to work without rails. So, I think the fix should go in rgviz-rails, but I am not coming up with a good way to do it. Thoughts?

Change to memory_executor breaks rgviz-rails

In memory_executor.rb line 15 when execute is defined. The definition was changed from:

def execute(query, options = {})

to:

def execute(query)

This causes line 30 of init.rb in rgviz-rails to fail because it is passing the opts parameter which causes an "Error in query: wrong number of arguments (2 for 1)" error. Please include the options parameter back in to be consistent with the other executors. Thanks.

"where column is null" results in a bad SQL query

If I have a query SELECT * WHERE column is null, the resulting SQL looks like: SELECT * FROM table WHERE "table"."column"is null Notice that there is no space between the column name and is null. Looks like postgres at least parses this correctly but I am not sure about other databases.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.