def expand_probability_estimate(probest, population, generator): simmodels = ast.SimulateModelsExp([ast.SelColExp(probest.expression, 'x')], population, generator) select = ast.Select(ast.SELQUANT_ALL, [ ast.SelColExp(ast.ExpApp(False, 'AVG', [ast.ExpCol(None, 'x')]), None) ], [ast.SelTab(simmodels, None)], None, None, None, None) return ast.ExpSub(select)
def test_multiquery(): assert parse_bql_string('select 0; select 1;') == [ ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitInt(0)), None)], None, None, None, None, None), ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitInt(1)), None)], None, None, None, None, None), ] assert parse_bql_string('select 0; select 1') == [ ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitInt(0)), None)], None, None, None, None, None), ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitInt(1)), None)], None, None, None, None, None), ]
def test_simulate_models_nontrivial(): # XXX test descent into ExpLit # XXX test descent into ExpNumpar # XXX test descent into ExpNampar # XXX test descent into ExpCol # XXX test descent into ExpSub # XXX test descent into ExpCollate # XXX test descent into ExpIn # XXX test descent into ExpCast # XXX test descent into ExpExists # XXX test descent into ExpApp # XXX test descent into ExpAppStar # XXX test descent into ExpCase mutinf0 = ast.ExpBQLMutInf(['c0'], ['c1', 'c2'], [('c3', ast.ExpLit(ast.LitInt(3)))], None) mutinf1 = ast.ExpBQLMutInf(['c4', 'c5'], ['c6'], [('c7', ast.ExpLit(ast.LitString('ergodic')))], 100) probdensity = ast.ExpBQLProbDensity( [('x', ast.ExpLit(ast.LitFloat(1.2)))], # No conditions for now -- that changes the weighting of the average. []) expression0 = ast.ExpOp(ast.OP_LT, [ mutinf0, ast.ExpOp(ast.OP_MUL, [ast.ExpLit(ast.LitFloat(0.1)), mutinf1]), ]) expression1 = probdensity simmodels = ast.SimulateModelsExp([ ast.SelColExp(expression0, 'quagga'), ast.SelColExp(expression1, 'eland'), ], 'p', 'g') assert macro.expand_simulate_models(simmodels) == \ ast.Select(ast.SELQUANT_ALL, [ ast.SelColExp( ast.ExpOp(ast.OP_LT, [ ast.ExpCol(None, 'v0'), ast.ExpOp(ast.OP_MUL, [ ast.ExpLit(ast.LitFloat(0.1)), ast.ExpCol(None, 'v1'), ]) ]), 'quagga'), ast.SelColExp(ast.ExpCol(None, 'v2'), 'eland'), ], [ast.SelTab( ast.SimulateModels( [ ast.SelColExp(mutinf0, 'v0'), ast.SelColExp(mutinf1, 'v1'), ast.SelColExp(probdensity, 'v2'), ], 'p', 'g'), None)], None, None, None, None)
def expand_simulate_models(sim): assert isinstance(sim, ast.SimulateModelsExp) if all( isinstance(c, ast.SelColExp) and (isinstance(c.expression, ast.ExpCol) or ast.is_bql(c.expression)) for c in sim.columns): return ast.SimulateModels(sim.columns, sim.population, sim.generator) simcols = [] selcols = [ c_ for c in sim.columns for c_ in _expand_simmodel_column(c, simcols) ] subsim = ast.SimulateModels(simcols, sim.population, sim.generator) seltab = ast.SelTab(subsim, None) return ast.Select(ast.SELQUANT_ALL, selcols, [seltab], None, None, None, None)
def test_expand_probability_estimate(): expression = ast.ExpOp(ast.OP_LT, [ ast.ExpBQLMutInf(['c0'], ['c1', 'c2'], [('c3', ast.ExpLit(ast.LitInt(3)))], None), ast.ExpLit(ast.LitFloat(0.1)), ]) probest = ast.ExpBQLProbEst(expression) assert macro.expand_probability_estimate(probest, 'p', 'g') == \ ast.ExpSub( ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpApp(False, 'AVG', [ast.ExpCol(None, 'x')]), None)], [ast.SelTab( ast.SimulateModelsExp([ast.SelColExp(expression, 'x')], 'p', 'g'), None)], None, None, None, None))
def test_parametrized(): assert parse_bql_string('select * from t where id = ?;') == \ [ast.Parametrized(ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'id'), ast.ExpNumpar(1), )), None, None, None), 1, {})] assert parse_bql_string('select * from t where id = ?123;') == \ [ast.Parametrized(ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'id'), ast.ExpNumpar(123), )), None, None, None), 123, {})] assert parse_bql_string('select * from t where id = :foo;') == \ [ast.Parametrized(ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'id'), ast.ExpNampar(1, ':foo'), )), None, None, None), 1, {':foo': 1})] assert parse_bql_string('select * from t where a = :foo and b = @foo;') \ == \ [ast.Parametrized(ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], ast.ExpOp(ast.OP_BOOLAND, ( ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'a'), ast.ExpNampar(1, ':foo'), )), ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'b'), ast.ExpNampar(2, '@foo'), )), )), None, None, None), 2, {':foo': 1, '@foo': 2})] assert parse_bql_string('select * from t where a = $foo and b = ?1;') == \ [ast.Parametrized(ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], ast.ExpOp(ast.OP_BOOLAND, ( ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'a'), ast.ExpNampar(1, '$foo'), )), ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'b'), ast.ExpNumpar(1), )), )), None, None, None), 1, {'$foo': 1})] assert parse_bql_string('select * from t' + ' where a = ?123 and b = :foo and c = ?124;') == \ [ast.Parametrized(ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], ast.ExpOp(ast.OP_BOOLAND, ( ast.ExpOp(ast.OP_BOOLAND, ( ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'a'), ast.ExpNumpar(123), )), ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'b'), ast.ExpNampar(124, ':foo'), )), )), ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'c'), ast.ExpNumpar(124), )), )), None, None, None), 124, {':foo': 124})]
def test_select_trivial(): assert parse_bql_string('select null;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitNull(None)), None)], None, None, None, None, None)] assert parse_bql_string("select 'x';") == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitString('x')), None)], None, None, None, None, None)] assert parse_bql_string("select 'x''y';") == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitString("x'y")), None)], None, None, None, None, None)] assert parse_bql_string('select "x";') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpCol(None, 'x'), None)], None, None, None, None, None)] assert parse_bql_string('select "x""y";') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpCol(None, 'x"y'), None)], None, None, None, None, None)] assert parse_bql_string('select 0;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitInt(0)), None)], None, None, None, None, None)] assert parse_bql_string('select 0.;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(0)), None)], None, None, None, None, None)] assert parse_bql_string('select .0;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(0)), None)], None, None, None, None, None)] assert parse_bql_string('select 0.0;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(0)), None)], None, None, None, None, None)] assert parse_bql_string('select 1e0;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(1)), None)], None, None, None, None, None)] assert parse_bql_string('select 1e+1;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(10)), None)], None, None, None, None, None)] assert parse_bql_string('select 1e-1;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(.1)), None)], None, None, None, None, None)] assert parse_bql_string('select 1.e0;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(1)), None)], None, None, None, None, None)] assert parse_bql_string('select .1e0;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(.1)), None)], None, None, None, None, None)] assert parse_bql_string('select .1e1;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(1)), None)], None, None, None, None, None)] assert parse_bql_string('select 1.e10;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitFloat(1e10)), None)], None, None, None, None, None)] assert parse_bql_string('select all 0;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitInt(0)), None)], None, None, None, None, None)] assert parse_bql_string('select distinct 0;') == \ [ast.Select(ast.SELQUANT_DISTINCT, [ast.SelColExp(ast.ExpLit(ast.LitInt(0)), None)], None, None, None, None, None)] assert parse_bql_string('select 0 as z;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitInt(0)), 'z')], None, None, None, None, None)] assert parse_bql_string('select * from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select t1.* from t1;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll('t1')], [ast.SelTab('t1', None)], None, None, None, None)] assert parse_bql_string('select c from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpCol(None, 'c'), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select c as d from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpCol(None, 'c'), 'd')], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select t.c as d from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpCol('t', 'c'), 'd')], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select t.c as d, p as q, x from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ ast.SelColExp(ast.ExpCol('t', 'c'), 'd'), ast.SelColExp(ast.ExpCol(None, 'p'), 'q'), ast.SelColExp(ast.ExpCol(None, 'x'), None), ], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select * from t, u;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None), ast.SelTab('u', None)], None, None, None, None)] assert parse_bql_string('select * from t as u;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', 'u')], None, None, None, None)] assert parse_bql_string('select * where x;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, ast.ExpCol(None, 'x'), None, None, None)] assert parse_bql_string('select * from t where x;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], ast.ExpCol(None, 'x'), None, None, None)] assert parse_bql_string('select * group by x;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, ast.Grouping([ast.ExpCol(None, 'x')], None), None, None)] assert parse_bql_string('select * from t where x group by y;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], ast.ExpCol(None, 'x'), ast.Grouping([ast.ExpCol(None, 'y')], None), None, None)] assert parse_bql_string('select * from t where x group by y, z;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], [ast.SelTab('t', None)], ast.ExpCol(None, 'x'), ast.Grouping([ast.ExpCol(None, 'y'), ast.ExpCol(None, 'z')], None), None, None)] assert parse_bql_string('select * order by x;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, None, [ast.Ord(ast.ExpCol(None, 'x'), ast.ORD_ASC)], None)] assert parse_bql_string('select * order by x asc;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, None, [ast.Ord(ast.ExpCol(None, 'x'), ast.ORD_ASC)], None)] assert parse_bql_string('select * order by x desc;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, None, [ast.Ord(ast.ExpCol(None, 'x'), ast.ORD_DESC)], None)] assert parse_bql_string('select * order by x, y;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, None, [ast.Ord(ast.ExpCol(None, 'x'), ast.ORD_ASC), ast.Ord(ast.ExpCol(None, 'y'), ast.ORD_ASC)], None)] assert parse_bql_string('select * order by x desc, y;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, None, [ast.Ord(ast.ExpCol(None, 'x'), ast.ORD_DESC), ast.Ord(ast.ExpCol(None, 'y'), ast.ORD_ASC)], None)] assert parse_bql_string('select * order by x, y asc;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, None, [ast.Ord(ast.ExpCol(None, 'x'), ast.ORD_ASC), ast.Ord(ast.ExpCol(None, 'y'), ast.ORD_ASC)], None)] assert parse_bql_string('select * limit 32;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, None, None, ast.Lim(ast.ExpLit(ast.LitInt(32)), None))] assert parse_bql_string('select * limit 32 offset 16;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, None, None, ast.Lim(ast.ExpLit(ast.LitInt(32)), ast.ExpLit(ast.LitInt(16))))] assert parse_bql_string('select * limit 16, 32;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColAll(None)], None, None, None, None, ast.Lim(ast.ExpLit(ast.LitInt(32)), ast.ExpLit(ast.LitInt(16))))] assert parse_bql_string('select (select0);') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpCol(None, 'select0'), None)], None, None, None, None, None)] assert parse_bql_string('select (select 0);') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpSub(ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpLit(ast.LitInt(0)), None)], None, None, None, None, None) ), None, )], None, None, None, None, None)] assert parse_bql_string('select f(f(), f(x), f(*), f(distinct x), y);') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpApp(False, 'f', [ ast.ExpApp(False, 'f', []), ast.ExpApp(False, 'f', [ast.ExpCol(None, 'x')]), ast.ExpAppStar('f'), ast.ExpApp(True, 'f', [ast.ExpCol(None, 'x')]), ast.ExpCol(None, 'y'), ]), None, )], None, None, None, None, None)]
def test_select_bql(): assert parse_bql_string('select predictive probability of c from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLPredProb('c'), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select predictive probability of c, * from t;') \ == \ [ast.Select(ast.SELQUANT_ALL, [ ast.SelColExp(ast.ExpBQLPredProb('c'), None), ast.SelColAll(None), ], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select c, predictive probability of d from t;') \ == \ [ast.Select(ast.SELQUANT_ALL, [ ast.SelColExp(ast.ExpCol(None, 'c'), None), ast.SelColExp(ast.ExpBQLPredProb('d'), None), ], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select predictive probability of c, d from t;') \ == \ [ast.Select(ast.SELQUANT_ALL, [ ast.SelColExp(ast.ExpBQLPredProb('c'), None), ast.SelColExp(ast.ExpCol(None, 'd'), None), ], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select probability of c = 42 from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLProb([('c', ast.ExpLit(ast.LitInt(42)))], []), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select similarity from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLSim(None, [ast.ColListAll()]), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select similarity to (rowid=8) from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpBQLSim( ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'rowid'), ast.ExpLit(ast.LitInt(8)) )), [ast.ColListAll()]), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select similarity with respect to c from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLSim(None, [ast.ColListLit(['c'])]), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string( 'select similarity to (rowid=8) with respect to c from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpBQLSim( ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'rowid'), ast.ExpLit(ast.LitInt(8)), )), [ast.ColListLit(['c'])]), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string( 'select similarity to (rowid=5) with respect to age from t1;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpBQLSim( ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'rowid'), ast.ExpLit(ast.LitInt(5)), )), [ast.ColListLit(['age'])]), None)], [ast.SelTab('t1', None)], None, None, None, None)] assert parse_bql_string( 'select similarity to (rowid=8) with respect to c, d from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ ast.SelColExp( ast.ExpBQLSim( ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'rowid'), ast.ExpLit(ast.LitInt(8)), )), [ast.ColListLit(['c'])]), None), ast.SelColExp(ast.ExpCol(None, 'd'), None), ], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select similarity to (rowid=8)' ' with respect to (c, d) from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpBQLSim( ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'rowid'), ast.ExpLit(ast.LitInt(8)), )), [ast.ColListLit(['c']), ast.ColListLit(['d'])]), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select similarity to (rowid=8) with respect to' + ' (estimate * from columns of t order by ' + ' probability of value 4 limit 1)' + ' from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpBQLSim( ast.ExpOp(ast.OP_EQ, ( ast.ExpCol(None, 'rowid'), ast.ExpLit(ast.LitInt(8)), )), [ast.ColListSub( ast.EstCols([ast.SelColAll(None)], 't', None, None, [ast.Ord(ast.ExpBQLProbFn( ast.ExpLit(ast.LitInt(4)), []), ast.ORD_ASC)], ast.Lim(ast.ExpLit(ast.LitInt(1)), None)) )]), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select dependence probability with c from t;') ==\ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLDepProb('c', None), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string( 'select dependence probability of c with d from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLDepProb('c', 'd'), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select mutual information with c from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLMutInf('c', None, None, None), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string( 'select mutual information of c with d from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLMutInf('c', 'd', None, None), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select mutual information of c with d' + ' using (1+2) samples from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpBQLMutInf( 'c', 'd', None, ast.op( ast.OP_ADD, ast.ExpLit(ast.LitInt(1)), ast.ExpLit(ast.LitInt(2)))), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string(''' select mutual information with c given (d, a=1) using 10 samples from t; ''') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpBQLMutInf( 'c', None, [('d', ast.ExpLit(ast.LitNull(0))), ('a',ast.ExpLit(ast.LitInt(1)))], ast.ExpLit(ast.LitInt(10)) ), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string(''' select mutual information of b with c given (d, a=1, e, r=2) from t;''') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp( ast.ExpBQLMutInf( 'b', 'c', [ ('d', ast.ExpLit(ast.LitNull(0))), ('a',ast.ExpLit(ast.LitInt(1))), ('e', ast.ExpLit(ast.LitNull(0))), ('r', ast.ExpLit(ast.LitInt(2))), ], None ), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select correlation with c from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLCorrel('c', None), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select correlation of c with d from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLCorrel('c', 'd'), None)], [ast.SelTab('t', None)], None, None, None, None)] # XXX This got broken a while ago: parenthesization in PROBABILITY # OF X = E is too permissive. I didn't notice because before I # introduced BQLParseError, this simply caught Exception -- which # covered the AssertionError that this turned into. # # with pytest.raises(parse.BQLParseError): # parse_bql_string('select probability of x = 1 -' + # ' probability of y = 0 from t;') # # XXX Should really be this test, but getting the grammar to # # admit this unambiguously is too much of a pain at the # # moment. # assert parse_bql_string('select probability of x = 1 -' + # ' probability of y = 0 from t;') == \ # [ast.Select(ast.SELQUANT_ALL, # [ast.SelColExp(ast.ExpBQLProb([('x', # ast.ExpOp(ast.OP_SUB, ( # ast.ExpLit(ast.LitInt(1)), # ast.ExpBQLProb([('y', # ast.ExpLit(ast.LitInt(0)))], # []), # )))], # []), # None)], # [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select probability of c1 = f(c2) from t;') == \ [ast.Select(ast.SELQUANT_ALL, [ast.SelColExp(ast.ExpBQLProb([('c1', ast.ExpApp(False, 'f', [ast.ExpCol(None, 'c2')]))], []), None)], [ast.SelTab('t', None)], None, None, None, None)] assert parse_bql_string('select key, t.(estimate * from columns of t' ' order by dependence probability with c desc limit 4)' ' from t order by key asc') == \ [ast.Select(ast.SELQUANT_ALL, [ ast.SelColExp(ast.ExpCol(None, 'key'), None), ast.SelColSub('t', ast.EstCols([ast.SelColAll(None)], 't', None, None, [ast.Ord(ast.ExpBQLDepProb('c', None), ast.ORD_DESC)], ast.Lim(ast.ExpLit(ast.LitInt(4)), None))) ], [ast.SelTab('t', None)], None, None, [ast.Ord(ast.ExpCol(None, 'key'), ast.ORD_ASC)], None)]
def p_select_s(self, quant, cols, tabs, cond, grouping, ord, lim): return ast.Select(quant, cols, tabs, cond, grouping, ord, lim)