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_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_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 p_primary_subquery(self, q): return ast.ExpSub(q)
def p_primary_subquery(self, q): return ast.ExpSub(q) def p_primary_cast(self, e, t): return ast.ExpCast(e, t)