Exemple #1
0
	def doMathFast(self, l, r, now):
		assert l.__class__ == SubstituteTable, "Left side of a join must be a SubstituteTable"
		assert r.__class__ == Node, "Right side of a join must be a Node"
		# ----------------------------------------
		_oprt = {
			"+": lambda x,y : int(x) + int(y),
			"-": lambda x,y : int(x) - int(y),
		}
		# ----------------------------------------
		mathNode = r
		lStt = l
		# ----------------------------------------
		lSttVars  = lStt.get_column_names()
		oprt = mathNode.pred
		oprtVars  = mathNode.atomArgs[:2]
		oprtConst = mathNode.atomArgs[2]

		retStt = SubstituteTable()
		#XXX: The order of the following two three lines MATTER
		for v in lSttVars:
			retStt.add_column_names([v])
		retStt.add_column_names(oprtVars)

		for t in range(self.lastSuccessfulJoin + 1, now + 1):
			for row in lStt.getRowsByCT(t):
				newRow = list(row)
				for v, vIdx in lSttVars.items():
					newRow[retStt.get_column_names()[v]] = row[vIdx]
				op = _oprt[oprt]
				newRow.append(op(row[lSttVars[oprtVars[1]]], oprtConst))
				retStt.add(tuple(newRow))

		return retStt
Exemple #2
0
	def doCompFast(self, l, r, now):
		assert l.__class__ == SubstituteTable, "Left side of a join must be a SubstituteTable"
		assert r.__class__ == Node, "Right side of a join must be a Node"

		# ----------------------------------------
		_oprt = {
			">": lambda x,y  : int(x) > int(y),
			"<": lambda x,y  : int(x) < int(y),
			"<=": lambda x,y : int(x) <= int(y),
			">=": lambda x,y : int(x) >= int(y),
			"=": lambda x,y : int(x) == int(y),
		}
		# ----------------------------------------
		compNode = r
		rStt = l
		# ----------------------------------------
		oprt = compNode.pred
		lOprndIsVar = compNode.atomArgs[0].isupper()
		if lOprndIsVar:
			lVar = compNode.atomArgs[0]
		else:
			lConstant = int(compNode.atomArgs[0])
		# ----------------------------------------
		rOprndIsVar = compNode.atomArgs[1].isupper()
		if rOprndIsVar:
			rVar = compNode.atomArgs[1]
		else:
			rConstant = int(compNode.atomArgs[1])
		# ----------------------------------------
		rSttVars = rStt.get_column_names()
		retStt = SubstituteTable()
		for v in rSttVars:
			retStt.add_column_names([v])
		# ----------------------------------------
		for t in range(self.lastSuccessfulJoin + 1, now + 1):
			for row in rStt.getRowsByCT(t):
				lValue = row[rSttVars[lVar]] if lOprndIsVar else lConstant
				rValue = row[rSttVars[rVar]] if rOprndIsVar else rConstant
				if _oprt[oprt](lValue, rValue):
					newRow = list(row)
					for v, vIdx in rSttVars.items():
						newRow[retStt.get_column_names()[v]] = row[vIdx]
					retStt.add(tuple(newRow))
		return retStt
Exemple #3
0
	def hashJoin(self, l, r, now):
		assert l.__class__ == SubstituteTable, "The left side of a join must be SubstitutionTable"
		if r.nodeType == Node.Math:
			return self.doMathSlow(l,r,now)
		if r.nodeType == Node.Comp:
			return self.doCompSlow(l,r,now)
		##############################
		lchStt = l
		rchStt = r.substitutetable
		##############################
		def key_gen(row, common_vars, column_idx):
			k = ""
			for v in common_vars:
				k += (row[column_idx[v]] if type(row[column_idx[v]]) is str else str(row[column_idx[v]]))

			return k
		##############################
		retStt = SubstituteTable()
		lch_vars = lchStt.get_column_names().keys()
		rch_vars = rchStt.get_column_names().keys()
		retStt.add_column_names(lch_vars)
		retStt.add_column_names(rch_vars)

		lch_vars_idx = lchStt.get_column_names()
		rch_vars_idx = rchStt.get_column_names()
		vars_idx = retStt.get_column_names()

		common_vars = list(set(lch_vars) & set(rch_vars))

		#print("================= %s ===================" % (str(common_vars)))
		#print("Return Var Idx = %s" % (str(vars_idx)))
		#print("xh1: %s" % (str(lchStt.get_column_names())))
		#print("xh2: %s" % (str(rchStt.get_column_names())))
		#print("CH1 STT = %s" % (str(lchStt)))
		#print("CH2 STT = %s" % (str(rchStt)))

		idx = dict()
		for t, rows in lchStt:
			for row in rows:
				k = key_gen(row, common_vars, lchStt.get_column_names())
				if k not in idx:
					idx[k] = [row]
				else:
					idx[k].append(row)

		for t, rows in rchStt:
			for row in rows:
				k = key_gen(row, common_vars, rchStt.get_column_names())
				if k in idx:
					for item in idx[k]:
						ct  = now
						ht = min(row[1], item[1])
						cc = None
						if (row[3] is not None) and (item[3] is not None):
							hc = min(row[3], item[3])
						elif row[3] is not None:
							hc = row[3]
						elif item[3] is not None:
							hc = item[3]
						else:
							hc = None

						new_row = [ct, ht, cc, hc] + [None] * (len(retStt.get_column_names()))
						for var,var_idx in vars_idx.items():
							if var in lch_vars_idx:
								new_row[var_idx] = item[lch_vars_idx[var]]
							elif var in rchStt.get_column_names():
								new_row[var_idx] = row[rch_vars_idx[var]]
							else:
								assert False, "This should never happen"
						#print("Adding Join row = %s" % (str(new_row)))
						retStt.add(tuple(new_row))

		return retStt