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 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 _expand_simmodel_exp(exp, simcols): if isinstance(exp, ast.ExpCol) or ast.is_bql(exp): tmpname = 'v%d' % (len(simcols), ) simcols.append(ast.SelColExp(exp, tmpname)) return ast.ExpCol(None, tmpname) elif isinstance(exp, ast.ExpLit) or \ isinstance(exp, ast.ExpNumpar) or \ isinstance(exp, ast.ExpNampar): return exp elif isinstance(exp, ast.ExpSub) or \ isinstance(exp, ast.ExpExists): # XXX Not really right -- need to provide correct scoping. return exp # XXX subquery scoping elif isinstance(exp, ast.ExpCollate): subexp = _expand_simmodel_exp(exp.expression, simcols) return ast.ExpCollate(subexp, exp.collation) elif isinstance(exp, ast.ExpInQuery): subexp = _expand_simmodel_exp(exp.expression, simcols) subquery = exp.subquery # XXX subquery scoping return ast.ExpIn(subexp, exp.positive, subquery) elif isinstance(exp, ast.ExpInExp): subexp = _expand_simmodel_exp(exp.expression, simcols) subexps = [_expand_simmodel_exp(se, simcols) for se in exp.expressions] return ast.ExpIn(subexp, exp.positive, subexps) elif isinstance(exp, ast.ExpCast): subexp = _expand_simmodel_exp(exp.expression, simcols) return ast.ExpCast(subexp, exp.type) elif isinstance(exp, ast.ExpApp): operands = [ _expand_simmodel_exp(operand, simcols) for operand in exp.operands ] return ast.ExpApp(exp.distinct, exp.operator, operands) elif isinstance(exp, ast.ExpAppStar): return exp elif isinstance(exp, ast.ExpCase): raise NotImplementedError("I'm too lazy to do CASE right now.") elif isinstance(exp, ast.ExpOp): operands = [ _expand_simmodel_exp(operand, simcols) for operand in exp.operands ] return ast.ExpOp(exp.operator, operands) else: assert False, 'Invalid expression: %s' % (repr(exp), )
def _expand_simmodel_exp(exp, simcols): if isinstance(exp, ast.ExpCol) or ast.is_bql(exp): tmpname = 'v%d' % (len(simcols),) simcols.append(ast.SelColExp(exp, tmpname)) return ast.ExpCol(None, tmpname) elif isinstance(exp, ast.ExpLit) or \ isinstance(exp, ast.ExpNumpar) or \ isinstance(exp, ast.ExpNampar): return exp elif isinstance(exp, ast.ExpSub) or \ isinstance(exp, ast.ExpExists): # XXX Not really right -- need to provide correct scoping. return exp # XXX subquery scoping elif isinstance(exp, ast.ExpCollate): subexp = _expand_simmodel_exp(exp.expression, simcols) return ast.ExpCollate(subexp, exp.collation) elif isinstance(exp, ast.ExpInQuery): subexp = _expand_simmodel_exp(exp.expression, simcols) subquery = exp.subquery # XXX subquery scoping return ast.ExpIn(subexp, exp.positive, subquery) elif isinstance(exp, ast.ExpInExp): subexp = _expand_simmodel_exp(exp.expression, simcols) subexps = [_expand_simmodel_exp(se, simcols) for se in exp.expressions] return ast.ExpIn(subexp, exp.positive, subexps) elif isinstance(exp, ast.ExpCast): subexp = _expand_simmodel_exp(exp.expression, simcols) return ast.ExpCast(subexp, exp.type) elif isinstance(exp, ast.ExpApp): operands = [_expand_simmodel_exp(operand, simcols) for operand in exp.operands] return ast.ExpApp(exp.distinct, exp.operator, operands) elif isinstance(exp, ast.ExpAppStar): return exp elif isinstance(exp, ast.ExpCase): raise NotImplementedError("I'm too lazy to do CASE right now.") elif isinstance(exp, ast.ExpOp): operands = [_expand_simmodel_exp(operand, simcols) for operand in exp.operands] return ast.ExpOp(exp.operator, operands) else: assert False, 'Invalid expression: %s' % (repr(exp),)
def test_is_bql(): assert ast.is_bql(ast.ExpLit(ast.LitInt(0))) == False assert ast.is_bql(ast.ExpNumpar(0)) == False assert ast.is_bql(ast.ExpNampar(0, 'x')) == False assert ast.is_bql(ast.ExpCol('t', 'c')) == False # ... assert ast.is_bql(ast.ExpBQLPredProb('c')) assert ast.is_bql(ast.ExpBQLProb([('c', ast.ExpLit(ast.LitInt(0)))], [])) assert ast.is_bql(ast.ExpBQLProbFn(ast.ExpLit(ast.LitInt(0)), [])) assert ast.is_bql(ast.ExpBQLSim(ast.ExpLit(ast.LitInt(0)), [])) assert ast.is_bql(ast.ExpBQLDepProb('c0', 'c1')) assert ast.is_bql(ast.ExpBQLMutInf('c0', 'c1', None, 100)) assert ast.is_bql(ast.ExpBQLCorrel('c0', 'c1')) assert ast.is_bql(ast.ExpBQLPredict('c', ast.ExpLit(ast.LitInt(.5)), None)) assert ast.is_bql(ast.ExpBQLPredictConf('c', None))
def test_is_bql(): assert ast.is_bql(ast.ExpLit(ast.LitInt(0))) == False assert ast.is_bql(ast.ExpNumpar(0)) == False assert ast.is_bql(ast.ExpNampar(0, 'x')) == False assert ast.is_bql(ast.ExpCol('t', 'c')) == False # ... assert ast.is_bql(ast.ExpBQLPredProb('c')) assert ast.is_bql(ast.ExpBQLProb([('c', ast.ExpLit(ast.LitInt(0)))], [])) assert ast.is_bql(ast.ExpBQLProbFn(ast.ExpLit(ast.LitInt(0)), [])) assert ast.is_bql(ast.ExpBQLSim(ast.ExpLit(ast.LitInt(0)), [])) assert ast.is_bql(ast.ExpBQLDepProb('c0', 'c1')) assert ast.is_bql(ast.ExpBQLMutInf('c0', 'c1', 100)) assert ast.is_bql(ast.ExpBQLCorrel('c0', 'c1')) assert ast.is_bql(ast.ExpBQLPredict('c', ast.ExpLit(ast.LitInt(0.5)))) assert ast.is_bql(ast.ExpBQLPredictConf('c'))