def visit_Attribute(self, node): p = self.visit(node.value) col_name = node.attr if col_name not in self.types: fail("Unknown Column name: \"%s\"" % col_name) c = Column(col_name, Table(p, [])) c.table = Table(p, []) return ColumnRef(c)
def test_table(): col1 = Column("col1") col2 = Column("col2") t = Table("t", [col1, col2]) assert t.columns[0] == col1 assert t.columns[1] == col2 assert col1.table == t assert col2.table == t with pytest.raises(Exception): Table(["col1", "col2"])
def test_derivedtable(): t1 = Table("t1", [Column("a"), ColumnExpr("c", Expression())]) t2 = Table("t2", [Column("b")]) col1 = Column("col1") col2 = Column("col2") e = Expression() dt = DerivedTable("dt", [col1, col2], [t1, t2], e, [col1]) assert dt.name == "dt" assert dt.columns[0].name == "col1" assert dt.columns[1].name == "col2" assert dt.base_tables == [t1, t2] assert dt.table_filter == e assert dt.groupby == [col1]
def test_collect_tables_for_pml(): ca = Column("a") cb = Column("b") cc = Column("c") cd = Column("d") ce = Column("e") cf = Column("f") t = Table("t", [ca, cb, cc, cd, ce, cf]) dt1_filter = Operator(LAND, [ Operator(NOTEQ, [ColumnRef(ca), ConstantExpr(0)]), Operator(GREATER, [ColumnRef(ca), ConstantExpr(5)]) ]) dt2_filter = Operator(LOR, [ Operator( LESS, [Operator(PLUS, [ColumnRef(cc), ConstantExpr(2)]), ColumnRef(cd)]), Operator(LESS_EQ, [ColumnRef(cc), ConstantExpr(5)]) ]) dt1 = DerivedTable("dt1", [ca, cb], [t], dt1_filter) dt2 = DerivedTable("dt2", [cc, cd], [t], dt2_filter, [cc]) dt3 = DerivedTable("dt3", [ca, cd], [dt1, dt2], None) pml = PlotModel("plot", "network", dt3, (ca, ), (cd, ), ["a", "d"], {'a': less_than(0.125)}) assert collect_tables_for_pml([pml]) == [t, dt1, dt2, dt3]
def test_column(): col = Column("test_col") t = Table("t", [col]) assert col.name == "test_col" col.table = t assert col.table == t assert t.columns[0] == col with pytest.raises(Exception): Column(3) Column("col").table = "table"
def test_derived2sql(): ca = Column("a") cb = Column("b") cc = Column("c") cd = Column("d") ce = Column("e") cf = Column("f") t = Table("t", [ca, cb, cc, cd, ce, cf]) dt1_filter = Operator(LAND, [ Operator(NOTEQ, [ColumnRef(ca), ConstantExpr(0)]), Operator(GREATER, [ColumnRef(ca), ConstantExpr(5)]) ]) dt2_filter = Operator(LOR, [ Operator( LESS, [Operator(PLUS, [ColumnRef(cc), ConstantExpr(2)]), ColumnRef(cd)]), Operator(LESS_EQ, [ColumnRef(cc), ConstantExpr(5)]) ]) dt1 = DerivedTable("dt1", [ca, cb], [t], dt1_filter) dt2 = DerivedTable("dt2", [cc, cd], [t], dt2_filter, [cc]) dt3_filter = Operator(LOR, [ Operator( LESS, [Operator(PLUS, [ColumnRef(ca), ConstantExpr(2)]), ColumnRef(cd)]), Operator(LESS_EQ, [ColumnRef(cd), ConstantExpr(5)]) ]) dt3 = DerivedTable("dt3", [dupc(ca), dupc(cd)], [dt1, dt2], dt3_filter, [ca]) assert derived2sql(dt1) == "SELECT a,b FROM t WHERE ((a <> 0) AND (a > 5))" assert derived2sql( dt2 ) == "SELECT c,d FROM t WHERE (((c + 2) < d) OR (c <= 5)) GROUP BY c" assert derived2sql( dt3 ) == "SELECT a,d FROM dt1,dt2 WHERE (((a + 2) < d) OR (d <= 5)) GROUP BY a"
def gen_table(self, d): """ generate a Table from specified dictionary d (the dictionary should represent only one table) then add the generated Table to derived_tables (a list of DerivedTable/Table) returns the Table """ if 'name' not in d: fail("Malformed data table is missing name!") missing_attr = [ key for key in ('columns', 'filename', 'format', 'node_mapping') if key not in d ] if missing_attr: fail("Malformed data table %s is missing keys %s", d['name'], ",".join(missing_attr)) cols = self.gen_columns(d["columns"], cols_of_data_table=True) dt = Table(d["name"], cols, d["filename"], d["format"], d["node_mapping"]) self.derived_tables.append(dt) return dt
def test_createviewforderived(): d = StatsDatabase(testing=True) alist = [ Attribute("a", "int"), Attribute("b", "int"), Attribute("c", "int"), Attribute("d", "int"), Attribute("e", "int"), Attribute("f", "int"), ] ca = Column("a") cb = Column("b") cc = Column("c") cd = Column("d") ce = Column("e") cf = Column("f") t = Table("t", [ca, cb, cc, cd, ce, cf]) d.create_table("t", alist) dt1_filter = Operator(LAND, [ Operator(NOTEQ, [ColumnRef(ca), ConstantExpr(0)]), Operator(GREATER, [ColumnRef(ca), ConstantExpr(5)]) ]) dt2_filter = Operator(LOR, [ Operator( LESS, [Operator(PLUS, [ColumnRef(cc), ConstantExpr(2)]), ColumnRef(cd)]), Operator(LESS_EQ, [ColumnRef(cc), ConstantExpr(5)]) ]) dt1 = DerivedTable("dt1", [ca, cb], [t], dt1_filter) dt2 = DerivedTable("dt2", [cc, cd], [t], dt2_filter, [cc]) dt3_filter = Operator(LAND, [ Operator(LOR, [ Operator(LESS, [ Operator(PLUS, [ColumnRef(ca), ConstantExpr(2)]), ColumnRef(cd) ]), Operator(LESS_EQ, [ColumnRef(cd), ConstantExpr(5)]) ]), Operator(EQ, [ColumnRef(ca), ColumnRef(cd)]) ]) dt3 = DerivedTable("dt3", [dupc(ca), dupc(cd)], [dt1, dt2], dt3_filter, [ca]) create_view_for_derived(d, dt1) create_view_for_derived(d, dt2) create_view_for_derived(d, dt3) assert d.relations["dt1"].name == "dt1" dt1_attr = d.get_attributes_of_relation("dt1") assert dt1_attr[0].name == "a" assert dt1_attr[1].name == "b" assert d.relations["dt2"].name == "dt2" dt2_attr = d.get_attributes_of_relation("dt2") assert dt2_attr[0].name == "c" assert dt2_attr[1].name == "d" assert d.relations["dt3"].name == "dt3" dt3_attr = d.get_attributes_of_relation("dt3") assert dt3_attr[0].name == "a" assert dt3_attr[1].name == "d"