2017-02-07 02:14:15 +00:00
import InfluxQL from 'src/influxql'
describe ( 'influxql astToString' , ( ) => {
it ( 'simple query' , ( ) => {
const ast = InfluxQL ( {
2017-06-07 16:23:53 +00:00
fields : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
column : {
expr : 'binary' ,
op : '+' ,
lhs : {
expr : 'literal' ,
val : '1' ,
type : 'integer' ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
rhs : {
expr : 'reference' ,
val : 'A' ,
} ,
} ,
} ,
2017-02-07 02:14:15 +00:00
] ,
2017-06-07 16:23:53 +00:00
sources : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
database : '' ,
retentionPolicy : '' ,
name : 'howdy' ,
type : 'measurement' ,
} ,
] ,
2017-02-07 02:14:15 +00:00
} )
const expected = ` SELECT 1 + "A" FROM "howdy" `
const actual = ast . toString ( )
// console.log(actual)
expect ( actual ) . to . equal ( expected )
} )
it ( 'simple query w/ multiple sources' , ( ) => {
const ast = InfluxQL ( {
2017-06-07 16:23:53 +00:00
fields : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
column : {
expr : 'binary' ,
op : '+' ,
lhs : {
expr : 'literal' ,
val : '1' ,
type : 'integer' ,
} ,
rhs : {
expr : 'reference' ,
val : 'A' ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
} ,
} ,
2017-02-07 02:14:15 +00:00
] ,
2017-06-07 16:23:53 +00:00
sources : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
database : '' ,
retentionPolicy : '' ,
name : 'howdy' ,
type : 'measurement' ,
2017-02-07 02:14:15 +00:00
} ,
{
2017-06-07 16:23:53 +00:00
database : 'telegraf' ,
retentionPolicy : 'autogen' ,
name : 'doody' ,
type : 'measurement' ,
} ,
] ,
2017-02-07 02:14:15 +00:00
} )
const expected = ` SELECT 1 + "A" FROM "howdy", "telegraf"."autogen"."doody" `
const actual = ast . toString ( )
// console.log('actual ', actual)
// console.log('expected', expected)
expect ( actual ) . to . equal ( expected )
} )
it ( 'query with AS' , ( ) => {
const ast = InfluxQL ( {
2017-06-07 16:23:53 +00:00
fields : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
alias : 'B' ,
column : {
expr : 'binary' ,
op : '+' ,
lhs : {
expr : 'literal' ,
val : '1' ,
type : 'integer' ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
rhs : {
expr : 'reference' ,
val : 'A' ,
} ,
} ,
} ,
2017-02-07 02:14:15 +00:00
] ,
2017-06-07 16:23:53 +00:00
sources : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
database : '' ,
retentionPolicy : '' ,
name : 'howdy' ,
type : 'measurement' ,
} ,
] ,
2017-02-07 02:14:15 +00:00
} )
const expected = ` SELECT 1 + "A" AS "B" FROM "howdy" `
const actual = ast . toString ( )
// console.log(actual)
expect ( actual ) . to . equal ( expected )
} )
it ( 'query with 2x func' , ( ) => {
const ast = InfluxQL ( {
2017-06-07 16:23:53 +00:00
fields : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
column : {
expr : 'binary' ,
op : '/' ,
lhs : {
expr : 'call' ,
name : 'derivative' ,
args : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
expr : 'reference' ,
val : 'field1' ,
2017-02-07 02:14:15 +00:00
} ,
{
2017-06-07 16:23:53 +00:00
expr : 'literal' ,
val : '1h' ,
type : 'duration' ,
} ,
] ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
rhs : {
expr : 'call' ,
name : 'derivative' ,
args : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
expr : 'reference' ,
val : 'field2' ,
2017-02-07 02:14:15 +00:00
} ,
{
2017-06-07 16:23:53 +00:00
expr : 'literal' ,
val : '1h' ,
type : 'duration' ,
} ,
] ,
} ,
} ,
} ,
2017-02-07 02:14:15 +00:00
] ,
2017-06-07 16:23:53 +00:00
sources : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
database : '' ,
retentionPolicy : '' ,
name : 'myseries' ,
type : 'measurement' ,
} ,
] ,
2017-02-07 02:14:15 +00:00
} )
const expected = ` SELECT derivative("field1", 1h) / derivative("field2", 1h) FROM "myseries" `
const actual = ast . toString ( )
// console.log('actual', actual)
// console.log('expected', expected)
expect ( actual ) . to . equal ( expected )
} )
it ( 'query with where and groupby' , ( ) => {
const ast = InfluxQL ( {
2017-06-07 16:23:53 +00:00
condition : {
expr : 'binary' ,
op : 'AND' ,
lhs : {
expr : 'binary' ,
op : 'AND' ,
lhs : {
expr : 'binary' ,
op : '=~' ,
lhs : {
expr : 'reference' ,
val : 'cluster_id' ,
} ,
rhs : {
expr : 'literal' ,
val : '/^23/' ,
type : 'regex' ,
2017-02-07 02:14:15 +00:00
} ,
} ,
2017-06-07 16:23:53 +00:00
rhs : {
expr : 'binary' ,
op : '=' ,
lhs : {
expr : 'reference' ,
val : 'host' ,
} ,
rhs : {
expr : 'literal' ,
val : 'prod-2ccccc04-us-east-1-data-3' ,
type : 'string' ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
} ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
rhs : {
expr : 'binary' ,
op : '\u003e' ,
lhs : {
expr : 'reference' ,
val : 'time' ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
rhs : {
expr : 'binary' ,
op : '-' ,
lhs : {
expr : 'call' ,
name : 'now' ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
rhs : {
expr : 'literal' ,
val : '15m' ,
type : 'duration' ,
} ,
} ,
} ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
fields : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
alias : 'max_cpus' ,
column : {
expr : 'call' ,
name : 'max' ,
args : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
expr : 'reference' ,
val : 'n_cpus' ,
} ,
] ,
} ,
2017-02-07 02:14:15 +00:00
} ,
{
2017-06-07 16:23:53 +00:00
column : {
expr : 'call' ,
name : 'non_negative_derivative' ,
args : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
expr : 'call' ,
name : 'median' ,
args : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
expr : 'reference' ,
val : 'n_users' ,
} ,
] ,
2017-02-07 02:14:15 +00:00
} ,
{
2017-06-07 16:23:53 +00:00
expr : 'literal' ,
val : '5m' ,
type : 'duration' ,
} ,
] ,
} ,
} ,
2017-02-07 02:14:15 +00:00
] ,
2017-06-07 16:23:53 +00:00
groupBy : {
time : {
interval : '15m' ,
offset : '10s' ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
tags : [ 'host' , 'tag_x' ] ,
fill : '10' ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
sources : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
database : '' ,
retentionPolicy : '' ,
name : 'system' ,
type : 'measurement' ,
} ,
] ,
2017-02-07 02:14:15 +00:00
} )
2017-06-07 16:23:53 +00:00
const expected =
'SELECT max("n_cpus") AS "max_cpus", non_negative_derivative(median("n_users"), 5m) FROM "system" WHERE "cluster_id" =~ /^23/ AND "host" = \'prod-2ccccc04-us-east-1-data-3\' AND time > now() - 15m GROUP BY time(15m, 10s),host,tag_x fill(10)'
2017-02-07 02:14:15 +00:00
const actual = ast . toString ( )
// console.log('actual ', actual)
// console.log('expected', expected)
expect ( actual ) . to . equal ( expected )
} )
it ( 'query with orderby and limit' , ( ) => {
const ast = InfluxQL ( {
2017-06-07 16:23:53 +00:00
condition : {
expr : 'binary' ,
op : 'AND' ,
lhs : {
expr : 'binary' ,
op : '=' ,
lhs : {
expr : 'reference' ,
val : 'host' ,
} ,
rhs : {
expr : 'literal' ,
val : 'hosta.influxdb.org' ,
type : 'string' ,
2017-02-07 02:14:15 +00:00
} ,
} ,
2017-06-07 16:23:53 +00:00
rhs : {
expr : 'binary' ,
op : '\u003e' ,
lhs : {
expr : 'reference' ,
val : 'time' ,
} ,
rhs : {
expr : 'literal' ,
val : '2017-02-07T01:43:02.245407693Z' ,
type : 'string' ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
} ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
fields : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
column : {
expr : 'call' ,
name : 'mean' ,
args : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
expr : 'reference' ,
val : 'field1' ,
} ,
] ,
} ,
2017-02-07 02:14:15 +00:00
} ,
{
2017-06-07 16:23:53 +00:00
column : {
expr : 'call' ,
name : 'sum' ,
args : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
expr : 'reference' ,
val : 'field2' ,
} ,
] ,
} ,
2017-02-07 02:14:15 +00:00
} ,
{
2017-06-07 16:23:53 +00:00
alias : 'field_x' ,
column : {
expr : 'call' ,
name : 'count' ,
args : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
expr : 'reference' ,
val : 'field3' ,
} ,
] ,
} ,
} ,
2017-02-07 02:14:15 +00:00
] ,
2017-06-07 16:23:53 +00:00
groupBy : {
time : {
interval : '10h' ,
} ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
limits : {
limit : 20 ,
offset : 10 ,
2017-02-07 02:14:15 +00:00
} ,
2017-06-07 16:23:53 +00:00
orderbys : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
name : 'time' ,
order : 'descending' ,
} ,
2017-02-07 02:14:15 +00:00
] ,
2017-06-07 16:23:53 +00:00
sources : [
2017-02-07 02:14:15 +00:00
{
2017-06-07 16:23:53 +00:00
database : '' ,
retentionPolicy : '' ,
name : 'myseries' ,
type : 'measurement' ,
} ,
] ,
2017-02-07 02:14:15 +00:00
} )
const expected = ` SELECT mean("field1"), sum("field2"), count("field3") AS "field_x" FROM "myseries" WHERE "host" = 'hosta.influxdb.org' AND time > '2017-02-07T01:43:02.245407693Z' GROUP BY time(10h) ORDER BY time DESC LIMIT 20 OFFSET 10 `
const actual = ast . toString ( )
// console.log('actual ', actual)
// console.log('expected', expected)
expect ( actual ) . to . equal ( expected )
} )
} )