def test_simulate(): with pytest.raises(parse.BQLParseError): # Need limit. parse_bql_string('create table s as simulate x from t') with pytest.raises(parse.BQLParseError): # Need limit. parse_bql_string('create table s as simulate x from t given y = 0') assert parse_bql_string('create table s as' ' simulate x from t limit 10') == \ [ast.CreateTabAs(False, False, 's', ast.Simulate( ['x'], 't', None, [], ast.ExpLit(ast.LitInt(10)), None) )] assert parse_bql_string('create table if not exists s as' ' simulate x, y from t given z = 0 limit 10 accuracy 2') == \ [ast.CreateTabAs(False, True, 's', ast.Simulate( ['x', 'y'], 't', None, [('z', ast.ExpLit(ast.LitInt(0)))], ast.ExpLit(ast.LitInt(10)), 2) )] assert parse_bql_string('create temp table s as' ' simulate x, y from t given z = 0 limit 10') == \ [ast.CreateTabAs(True, False, 's', ast.Simulate( ['x', 'y'], 't', None, [('z', ast.ExpLit(ast.LitInt(0)))], ast.ExpLit(ast.LitInt(10)), None), )] assert parse_bql_string( 'create temp table if not exists s as' ' simulate x, y from t given z = 0, w = 1' ' limit 10 accuracy 19') == \ [ast.CreateTabAs(True, True, 's', ast.Simulate( ['x', 'y'], 't', None, [ ('z', ast.ExpLit(ast.LitInt(0))), ('w', ast.ExpLit(ast.LitInt(1))), ], ast.ExpLit(ast.LitInt(10)), 19) )] # Specifying a quantity other than a variable should raise. with pytest.raises(parse.BQLParseError): parse_bql_string( 'simulate a, dependence probability of a with b from t limit 10;') with pytest.raises(parse.BQLParseError): parse_bql_string( 'create table f as simulate a, dependence probability of a with b ' 'from t limit 10;')
def test_trivial_commands(): assert parse_bql_string(''' create population satellites for satellites_ucs ( MODEL country_of_operator, orbit_type AS categorical; MODEL launch_mass AS numerical; MODEL perigee AS numerical; MODEL apogee, period AS numerical ) ''') == \ [ast.CreatePop(False, 'satellites', 'satellites_ucs', [ ast.PopModelVars( ['country_of_operator', 'orbit_type'], 'categorical'), ast.PopModelVars(['launch_mass'], 'numerical'), ast.PopModelVars(['perigee'], 'numerical'), ast.PopModelVars(['apogee', 'period'], 'numerical'), ])] assert parse_bql_string('drop population satellites') == \ [ast.DropPop(False, 'satellites')] assert parse_bql_string('create generator t_cc for t using crosscat' '(xyz numerical, pqr categorical, lmn cyclic)') == \ [ast.CreateGen('t_cc', False, 't', None, 'crosscat', [ ['xyz', 'numerical'], ['pqr', 'categorical'], ['lmn', 'cyclic'], ])] assert parse_bql_string('create generator t_cc for t with baseline crosscat' '(xyz numerical, pqr categorical, lmn cyclic)') == \ [ast.CreateGen( 't_cc', False, 't', ast.Baseline('crosscat', []), None, # Defaults to cgpm. [ ['xyz', 'numerical'], ['pqr', 'categorical'], ['lmn', 'cyclic'], ])] assert parse_bql_string('create generator t_cc if not exists' ' for t using crosscat' '(xyz numerical, pqr categorical, lmn cyclic)') == \ [ast.CreateGen('t_cc', True, 't', None, 'crosscat', [ ['xyz', 'numerical'], ['pqr', 'categorical'], ['lmn', 'cyclic'], ])] assert parse_bql_string('create generator if not exists t_cc' ' for t using crosscat' '(xyz numerical, pqr categorical, lmn cyclic)') == \ [ast.CreateGen('t_cc', True, 't', None, 'crosscat', [ ['xyz', 'numerical'], ['pqr', 'categorical'], ['lmn', 'cyclic'], ])] # XXX Schema of [[]] instead of [] is kinda wacky. Fix? (But # make sure the empty-parens and no-parens cases are equivalent.) assert parse_bql_string('create generator t_cc' ' for t using crosscat()') == \ [ast.CreateGen('t_cc', False, 't', None, 'crosscat', [[]])] assert parse_bql_string('create generator t_cc' ' for t using crosscat') == \ [ast.CreateGen('t_cc', False, 't', None, 'crosscat', [[]])] assert parse_bql_string('initialize 1 model for t;') == \ [ast.InitModels(False, 't', 1)] assert parse_bql_string('initialize 1 model if not exists for t;') == \ [ast.InitModels(True, 't', 1)] assert parse_bql_string('initialize 2 models for t;') == \ [ast.InitModels(False, 't', 2)] assert parse_bql_string('initialize 2 models if not exists for t;') == \ [ast.InitModels(True, 't', 2)] assert parse_bql_string('create temporary table tx as' ' infer explicit x, predict x as xi confidence xc from t_cc') == \ [ast.CreateTabAs(True, False, 'tx', ast.InferExplicit( [ ast.SelColExp(ast.ExpCol(None, 'x'), None), ast.PredCol('x', 'xi', 'xc', None), ], 't_cc', None, None, None, None, None, ))]
def test_trivial_commands(): assert parse_bql_string('create generator t_cc for t using crosscat' '(xyz numerical, pqr categorical, lmn cyclic)') == \ [ast.CreateGen(False, 't_cc', False, 't', 'crosscat', [ ['xyz', 'numerical'], ['pqr', 'categorical'], ['lmn', 'cyclic'], ])] assert parse_bql_string('create default generator t_cc for t using crosscat' '(xyz numerical, pqr categorical, lmn cyclic)') == \ [ast.CreateGen(True, 't_cc', False, 't', 'crosscat', [ ['xyz', 'numerical'], ['pqr', 'categorical'], ['lmn', 'cyclic'], ])] assert parse_bql_string('create generator t_cc if not exists' ' for t using crosscat' '(xyz numerical, pqr categorical, lmn cyclic)') == \ [ast.CreateGen(False, 't_cc', True, 't', 'crosscat', [ ['xyz', 'numerical'], ['pqr', 'categorical'], ['lmn', 'cyclic'], ])] assert parse_bql_string('initialize 1 model for t;') == \ [ast.InitModels(False, 't', 1, None)] assert parse_bql_string('initialize 1 model if not exists for t;') == \ [ast.InitModels(True, 't', 1, None)] assert parse_bql_string('initialize 2 models for t;') == \ [ast.InitModels(False, 't', 2, None)] assert parse_bql_string('initialize 2 models if not exists for t;') == \ [ast.InitModels(True, 't', 2, None)] assert parse_bql_string('analyze t for 1 iteration;') == \ [ast.AnalyzeModels('t', None, 1, None, None, None, False)] assert parse_bql_string('analyze t for 1 iteration wait;') == \ [ast.AnalyzeModels('t', None, 1, None, None, None, True)] assert parse_bql_string('analyze t for 1 minute;') == \ [ast.AnalyzeModels('t', None, None, 60, None, None, False)] assert parse_bql_string('analyze t for 1 minute wait;') == \ [ast.AnalyzeModels('t', None, None, 60, None, None, True)] assert parse_bql_string('analyze t for 2 minutes;') == \ [ast.AnalyzeModels('t', None, None, 120, None, None, False)] assert parse_bql_string('analyze t for 2 minutes wait;') == \ [ast.AnalyzeModels('t', None, None, 120, None, None, True)] assert parse_bql_string('analyze t for 1 second;') == \ [ast.AnalyzeModels('t', None, None, 1, None, None, False)] assert parse_bql_string('analyze t for 1 second wait;') == \ [ast.AnalyzeModels('t', None, None, 1, None, None, True)] assert parse_bql_string('analyze t for 2 seconds;') == \ [ast.AnalyzeModels('t', None, None, 2, None, None, False)] assert parse_bql_string('analyze t for 2 seconds wait;') == \ [ast.AnalyzeModels('t', None, None, 2, None, None, True)] assert parse_bql_string('analyze t model 1 for 1 iteration;') == \ [ast.AnalyzeModels('t', [1], 1, None, None, None, False)] assert parse_bql_string('analyze t models 1,2,3 for 1 iteration;') == \ [ast.AnalyzeModels('t', [1,2,3], 1, None, None, None, False)] assert parse_bql_string('analyze t models 1-3,5 for 1 iteration;') == \ [ast.AnalyzeModels('t', [1,2,3,5], 1, None, None, None, False)] assert parse_bql_string('analyze t for 10 iterations' ' checkpoint 3 iterations') == \ [ast.AnalyzeModels('t', None, 10, None, 3, None, False)] assert parse_bql_string('analyze t for 10 seconds' ' checkpoint 3 seconds') == \ [ast.AnalyzeModels('t', None, None, 10, None, 3, False)] assert parse_bql_string('create temporary table tx as' ' infer explicit x, predict x as xi confidence xc from t_cc') == \ [ast.CreateTabAs(True, False, 'tx', ast.InferExplicit( [ ast.SelColExp(ast.ExpCol(None, 'x'), None), ast.PredCol('x', 'xi', 'xc'), ], 't_cc', ast.ExpLit(ast.LitNull(None)), None, None, None, None, ))]
def p_command_createtab_as(self, temp, ifnotexists, name, query): if isinstance(query, ast.SimulateModels): return ast.CreateTabSimModels(temp, ifnotexists, name, query) else: return ast.CreateTabAs(temp, ifnotexists, name, query)
def p_command_createtab_as(self, temp, ifnotexists, name, query): return ast.CreateTabAs(temp, ifnotexists, name, query)