Пример #1
0
 def group(self, attrIdx, count=False, aggrAttrIdx=[], aggrFunc=[]):
     #TODO: remove attributes of intermediary tables in attribute store
     attrIdx, aggrAttrIdx, aggrFunc = util.makelist(attrIdx, aggrAttrIdx,
                                                    aggrFunc)
     assert len(aggrAttrIdx) == len(aggrFunc)
     tmptable, idxmap = self.copy()
     aggrAttrIdx = util.mapIdx(aggrAttrIdx, idxmap)
     if count:
         cntIdx = tmptable.addAttr(gsql.WEIGHT_ATTR_NAME, val=Value(val=1))
         aggrAttrIdx.append(cntIdx)
         aggrFunc.append('cnt')
     # Find values for aggregation
     agg = aggregator.Aggregator(aggrFunc)
     aggCols = [tmptable.getColumn(idx) for idx in aggrAttrIdx]
     # Find groups of rows, and corresponding list of aggregation attributes
     tproj, _ = tmptable.project(attrIdx)
     groups = {}
     for i, row in enumerate(tproj.data):
         key = tuple(row)
         if not key in groups:
             groups[key] = []
         groups[key].append([col[i] for col in aggCols])
         # groups[key] is a list of lists: each inner list is the list of
         # aggregation values corresponding to this row
     # Create final table
     tfinal, _ = tmptable.project(attrIdx + aggrAttrIdx)
     for key in groups:
         aggvals = agg.calc(groups[key])
         newrow = list(key) + aggvals
         tfinal.data.append(newrow)
     idxmap = dict(zip(attrIdx + aggrAttrIdx, tfinal.columns))
     return tfinal, idxmap
Пример #2
0
	def group(self, attrIdx, count=False, aggrAttrIdx=[], aggrFunc=[]):
		#TODO: remove attributes of intermediary tables in attribute store
		attrIdx,aggrAttrIdx,aggrFunc = util.makelist(attrIdx,aggrAttrIdx,aggrFunc)
		assert len(aggrAttrIdx) == len(aggrFunc)
		tmptable,idxmap = self.copy()
		aggrAttrIdx = util.mapIdx(aggrAttrIdx,idxmap)
		if count:
			cntIdx = tmptable.addAttr(gsql.WEIGHT_ATTR_NAME,val=Value(val=1))
			aggrAttrIdx.append(cntIdx)
			aggrFunc.append('cnt')
		# Find values for aggregation
		agg = aggregator.Aggregator(aggrFunc)
		aggCols = [tmptable.getColumn(idx) for idx in aggrAttrIdx]
		# Find groups of rows, and corresponding list of aggregation attributes
		tproj,_ = tmptable.project(attrIdx)
		groups = {}
		for i,row in enumerate(tproj.data):
			key = tuple(row)
			if not key in groups:
				groups[key] = []
			groups[key].append([col[i] for col in aggCols])
			# groups[key] is a list of lists: each inner list is the list of
			# aggregation values corresponding to this row
		# Create final table
		tfinal,_ = tmptable.project(attrIdx+aggrAttrIdx)
		for key in groups:
			aggvals = agg.calc(groups[key])
			newrow = list(key) + aggvals
			tfinal.data.append(newrow)
		idxmap = dict(zip(attrIdx+aggrAttrIdx,tfinal.columns))
		return tfinal,idxmap
Пример #3
0
 def getPos(self, index):
     # TODO: raise error if index not found
     indexlist, = util.makelist(index)
     positions = [-1] * len(indexlist)
     for i, idx1 in enumerate(indexlist):
         for pos, idx2 in enumerate(self.columns):
             if idx1 == idx2:
                 positions[i] = pos
     if isinstance(index, (list, tuple)):
         return positions
     else:
         return positions[0]
Пример #4
0
	def getPos(self,index):
		# TODO: raise error if index not found
		indexlist, = util.makelist(index)
		positions = [-1]*len(indexlist)
		for i,idx1 in enumerate(indexlist):
			for pos,idx2 in enumerate(self.columns):
				if idx1 == idx2:
					positions[i] = pos
		if isinstance(index,(list,tuple)):
			return positions
		else:
			return positions[0]
Пример #5
0
 def getIndex(self, attr):
     attrlist, = util.makelist(attr)
     result = []
     for a in attrlist:
         indexes = self.getAllIdxForAttr(a)
         if len(indexes) > 1:
             raise TableStructureError('Ambiguous attribute name')
         elif len(indexes) <= 0:
             raise TableAttrNotFoundError('Attribute not found in table')
         result.append(indexes[0])
     if isinstance(attr, basestring):
         return result[0]
     return result
Пример #6
0
	def select(self, condition, projIdx=None):
		if projIdx == None:
			projIdx = self.columns
		condition,projIdx = util.makelist(condition,projIdx)
		for c in condition:
			c.configureForTable(self)
		table = self.newTable()
		f = lambda row: reduce(__and__,[c.eval(row) for c in condition],True)
		table.data = [row[:] for row in (filter(f,self.data))]
		newProjIdx = [newIdx for oldIdx,newIdx in zip(self.columns,table.columns) if oldIdx in projIdx]
		tfinal,_ = table.project(newProjIdx)
		idxmap = dict(zip(projIdx,tfinal.columns))
		return tfinal,idxmap
Пример #7
0
	def getIndex(self,attr):
		attrlist, = util.makelist(attr)
		result = []
		for a in attrlist:
			indexes = self.getAllIdxForAttr(a)
			if len(indexes) > 1:
				raise TableStructureError('Ambiguous attribute name')
			elif len(indexes) <= 0:
				raise TableAttrNotFoundError('Attribute not found in table')
			result.append(indexes[0])
		if isinstance(attr,basestring):
			return result[0]
		return result
Пример #8
0
 def select(self, condition, projIdx=None):
     if projIdx == None:
         projIdx = self.columns
     condition, projIdx = util.makelist(condition, projIdx)
     for c in condition:
         c.configureForTable(self)
     table = self.newTable()
     f = lambda row: reduce(__and__, [c.eval(row) for c in condition], True)
     table.data = [row[:] for row in (filter(f, self.data))]
     newProjIdx = [
         newIdx for oldIdx, newIdx in zip(self.columns, table.columns)
         if oldIdx in projIdx
     ]
     tfinal, _ = table.project(newProjIdx)
     idxmap = dict(zip(projIdx, tfinal.columns))
     return tfinal, idxmap