Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(query_result): implement untyped parser
- Loading branch information
1 parent
c9273f0
commit ee0f429
Showing
6 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "../spec_helper" | ||
|
||
describe Flux::QueryResult do | ||
describe ".from" do | ||
io = IO::Memory.new <<-CSV | ||
#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,string,string,double | ||
#group,false,false,false,false,false,false,false,false | ||
#default,,,,,,,, | ||
,result,table,_start,_stop,_time,region,host,_value | ||
,my-result,0,2018-05-08T20:50:00Z,2018-05-08T20:51:00Z,2018-05-08T20:50:00Z,east,A,15.43 | ||
,my-result,0,2018-05-08T20:50:00Z,2018-05-08T20:51:00Z,2018-05-08T20:50:20Z,east,B,59.25 | ||
,my-result,0,2018-05-08T20:50:00Z,2018-05-08T20:51:00Z,2018-05-08T20:50:40Z,east,C,52.62 | ||
,my-result,1,2018-05-08T20:50:00Z,2018-05-08T20:51:00Z,2018-05-08T20:50:00Z,west,A,62.73 | ||
,my-result,1,2018-05-08T20:50:00Z,2018-05-08T20:51:00Z,2018-05-08T20:50:20Z,west,B,12.83 | ||
,my-result,1,2018-05-08T20:50:00Z,2018-05-08T20:51:00Z,2018-05-08T20:50:40Z,west,C,51.62 | ||
CSV | ||
|
||
Flux::QueryResult.parse io | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "./annotated_csv" | ||
require "./query_result/*" | ||
|
||
# Parsers and data structures for working with returned query results. | ||
module Flux::QueryResult | ||
extend self | ||
|
||
# FIXME: support query results containing mutliple schemas | ||
def parse(io : IO) : Enumerable(Table(Array(String))) | ||
tables = [] of Table(Array(String)) | ||
|
||
AnnotatedCSV.new(io, headers: true).each do |csv| | ||
idx = csv["table"].to_i | ||
table = tables[idx]? | ||
|
||
unless table | ||
columns = csv.headers.map do |name| | ||
meta = csv.annotations name | ||
Column.new( | ||
name: name, | ||
type: DataType.parse(meta["datatype"]), | ||
group: meta["group"] == "true", | ||
default: meta["default"] | ||
) | ||
end | ||
table = Table(Array(String)).new columns | ||
tables << table | ||
end | ||
|
||
table << csv.row.to_a | ||
end | ||
|
||
tables | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "./data_type" | ||
|
||
struct Flux::QueryResult::Column | ||
getter name : String | ||
|
||
getter type : DataType | ||
|
||
getter group : Bool | ||
|
||
getter default : String | ||
|
||
def initialize(@name, @type, @group, @default) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
enum Flux::QueryResult::DataType | ||
Annotation | ||
Boolean | ||
UnsignedLong | ||
Long | ||
Double | ||
String | ||
Base64Binary | ||
DateTime | ||
Duration | ||
|
||
def self.parse(string : ::String) : self | ||
if string.starts_with? '#' | ||
Annotation | ||
else | ||
# Types can have an optional encodeding appended. This appears to be | ||
# static though? e.g `dateTime:RFC3339`. | ||
super string.split(':')[0] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "./column" | ||
|
||
struct Flux::QueryResult::Table(T) | ||
include Indexable(T) | ||
|
||
getter columns : Array(Column) | ||
|
||
private getter records = [] of T | ||
|
||
def initialize(@columns) | ||
end | ||
|
||
delegate each, :<<, unsafe_fetch, to: records | ||
|
||
def group_key | ||
columns.select(&.group).map(&.name) | ||
end | ||
end |