def execute(self, environment): try: exp1 = self.exp1.execute(environment) exp2 = self.exp2.execute(environment) operator = self.operator if exp1.type != exp.TYPE.STRING and exp2.type != exp.TYPE.STRING: exp.list_errors.append( "Error: 42883: la operacion no existe entre: " + str(exp1.type) + " " + str(operator) + " " + str(exp2.type) + "\n En la linea: " + str(self.row)) raise Exception if isinstance(exp1.value, pd.core.series.Series): exp1.value = exp1.value.apply(str) else: exp1.value = str(exp1.value) if isinstance(exp2.value, pd.core.series.Series): exp2.value = exp2.value.apply(str) else: exp2.value = str(exp2.value) if operator == "||": value = exp1.value + exp2.value else: exp.list_errors.append( "Error: 42725: el operador no es unico: " + str(exp1.type) + " " + str(operator) + " " + str(exp2.type) + "\n En la linea: " + str(self.row)) raise Exception return primitive.Primitive(exp.TYPE.STRING, value, self.temp, self.row, self.column) except: raise exp.list_errors.append( "Error: XX000: Error interno (Binary String Operation)" + "\n En la linea: " + str(self.row))
def execute(self, environment): try: df1 = environment.dataFrame.copy() names = {} for n in list(df1.columns): names[n] = n.split(".")[1] df1.rename(columns=names, inplace=True) df2 = self.subquery.execute(environment)[0] y = df1.columns.intersection(df2.columns) lst = list(y) if len(lst) < 1: list_errors.append( "Error: 42P10: Referencia de columnas invalidas EXIST" + "\n En la linea: " + str(self.row)) value = (df1[lst].apply(tuple, 1).isin(df2[lst].apply(tuple, 1))) return primitive.Primitive(exp.TYPE.BOOLEAN, value, self.temp, self.row, self.column) except: list_errors.append( "Error: XX000: Error interno (Exist Relational Operation)" + "\n En la linea: " + str(self.row))
def execute(self, environment): exp1 = self.exp1.execute(environment) exp2 = self.exp2.execute(environment) operator = self.operator try: if operator == "<": value = exp1.value < exp2.value elif operator == ">": value = exp1.value > exp2.value elif operator == ">=": value = exp1.value >= exp2.value elif operator == "<=": value = exp1.value <= exp2.value elif operator == "=": value = exp1.value == exp2.value elif operator == "!=": value = exp1.value != exp2.value elif operator == "<>": value = exp1.value != exp2.value elif operator == "ISDISTINCTFROM": value = exp1.value != exp2.value elif operator == "ISNOTDISTINCTFROM": value = exp1.value == exp2.value else: exp.list_errors.append( "Error: 22P02: entrada invalida: " + str(exp1.type) + " " + str(operator) + " " + str(exp2.type) + "\n En la linea: " + str(self.row) ) raise Exception return primitive.Primitive( exp.TYPE.BOOLEAN, value, self.temp, self.row, self.column ) except TypeError: exp.list_errors.append( "Error: 42883: la operacion no existe entre: " + str(exp1.type) + " " + str(operator) + " " + str(exp2.type) + "\n En la linea: " + str(self.row) ) raise Exception except: raise exp.list_errors.append( "Error: XX000: Error interno (Binary Relational Operation)" + "\n En la linea: " + str(self.row) )
def execute(self, environment): exp = self.exp.execute(environment) operator = self.operator try: if exp.type != exp.TYPE.BOOLEAN: raise TypeError if isinstance(exp.value, pd.core.series.Series): if operator == "NOT": value = ~exp.value elif operator == "ISTRUE": value = exp.value == True elif operator == "ISFALSE": value = exp.value == False elif operator == "ISUNKNOWN": value = exp.value == None elif operator == "ISNOTTRUE": value = exp.value != True elif operator == "ISNOTFALSE": value = exp.value != False elif operator == "ISNOTUNKNOWN": value = exp.value != None else: raise TypeError else: if operator == "NOT": value = not exp.value elif operator == "ISTRUE": value = exp.value == True elif operator == "ISFALSE": value = exp.value == False elif operator == "ISUNKNOWN": value = exp.value == None elif operator == "ISNOTTRUE": value = exp.value != True elif operator == "ISNOTFALSE": value = exp.value != False elif operator == "ISNOTUNKNOWN": value = exp.value != None else: raise TypeError return primitive.Primitive(exp.TYPE.BOOLEAN, value, self.temp, self.row, self.column) except TypeError: raise exp.list_errors.append( "Error: 42883: la operacion no existe entre: " + str(exp.type) + " y el operador " + str(operator) + "\n En la linea: " + str(self.row)) except: raise exp.list_errors.append( "Error: XX000: Error interno (Binary Aritmethic Operation)" + "\n En la linea: " + str(self.row))
def execute(self, environment): exp1 = self.exp1.execute(environment) exp2 = self.exp2.execute(environment) exp3 = self.exp3.execute(environment) operator = self.operator try: if (isinstance(exp1.value, pd.core.series.Series) or isinstance(exp2.value, pd.core.series.Series) or isinstance(exp3.value, pd.core.series.Series)): if operator == "BETWEEN": value = (exp1.value > exp2.value) & (exp1.value < exp3.value) elif operator == "NOTBETWEEN": value = not ((exp1.value > exp2.value) & (exp1.value < exp3.value)) elif operator == "BETWEENSYMMETRIC": t1 = (exp1.value > exp2.value) & (exp1.value < exp3.value) t2 = (exp1.value < exp2.value) & (exp1.value > exp3.value) value = t1 | t2 else: exp.list_errors.append("Error: 42601: Error sintactico: " + "\n En la linea: " + str(self.row)) return AssertionError else: if operator == "BETWEEN": value = exp1.value > exp2.value and exp1.value < exp3.value elif operator == "NOTBETWEEN": value = not (exp1.value > exp2.value and exp1.value < exp3.value) elif operator == "BETWEENSYMMETRIC": t1 = exp1.value > exp2.value and exp1.value < exp3.value t2 = exp1.value < exp2.value and exp1.value > exp3.value value = t1 or t2 else: exp.list_errors.append("Error: 42601: Error sintactico: " + "\n En la linea: " + str(self.row)) return AssertionError return primitive.Primitive(exp.TYPE.BOOLEAN, value, self.temp, self.row, self.column) except TypeError: exp.list_errors.append( "Error: 42883: la operacion no existe entre: " + str(exp1.type) + " " + str(operator) + " " + str(exp2.type) + " y " + str(exp3.type) + "\n En la linea: " + str(self.row)) return AssertionError except: exp.list_errors.append( "Error: XX000: Error interno (Ternary Relational Operation)" + "\n En la linea: " + str(self.row)) pass
def execute(self, environment): col = self.colData.execute(environment) df = self.subquery.execute(environment)[0] # TODO: Falta agregar la verificacion de types if len(list(df.columns)) != 1: list_errors.append( "Error: XX000: Error interno (Exist Relational Operation)" + "\n En la linea: " + str(self.row)) value = col.value.isin(df.iloc[:, 0]) if self.optNot == "NOT": value = ~value return primitive.Primitive(exp.TYPE.BOOLEAN, value, self.temp, self.row, self.column)
def execute(self, environment): exp = self.exp.execute(environment) operator = self.operator try: if operator == "ISNULL": value = exp.value == None elif operator == "NOTNULL": value = exp.value != None elif operator == "ISTRUE": value = exp.value == True elif operator == "ISFALSE": value = exp.value == False elif operator == "ISUNKNOWN": value = exp.value == None elif operator == "ISNOTNULL": value = exp.value != None elif operator == "ISNOTTRUE": value = exp.value != True elif operator == "ISNOTFALSE": value = exp.value != False elif operator == "ISNOTUNKNOWN": value = exp.value != None else: raise TypeError return primitive.Primitive( exp.TYPE.BOOLEAN, value, self.temp, self.row, self.column ) except TypeError: raise exp.list_errors.append( "Error: 42883: la operacion no existe entre: " + str(exp.type) + " " + str(operator) + " " + "\n En la linea: " + str(self.row) ) except: raise exp.list_errors.append( "Error: XX000: Error interno (Unary Relational Operation)" + "\n En la linea: " + str(self.row) )
def execute(self, environment): exp1 = self.exp1.execute(environment) exp2 = self.exp2.execute(environment) operator = self.operator try: if exp1.type != exp.TYPE.NUMBER or exp2.type != exp.TYPE.NUMBER: raise TypeError if operator == "+": value = exp1.value + exp2.value elif operator == "-": value = exp1.value - exp2.value elif operator == "*": value = exp1.value * exp2.value elif operator == "/": if exp2.value == 0: exp.list_errors.append( "Error: 22012: No se puede dividir por cero") value = 0 else: value = exp1.value / exp2.value elif operator == "^": value = exp1.value**exp2.value elif operator == "%": if exp2.value == 0: exp.list_errors.append( "Error: 22012: No se puede modular por cero") value = 0 else: value = exp1.value % exp2.value else: raise TypeError return primitive.Primitive(exp.TYPE.NUMBER, value, self.temp, self.row, self.column) except TypeError: raise exp.list_errors.append( "Error: 42883: la operacion no existe entre: " + str(exp1.type) + " " + str(operator) + " " + str(exp2.type) + "\n En la linea: " + str(self.row)) except: raise exp.list_errors.append( "Error: XX000: Error interno (Binary Aritmethic Operation)" + "\n En la linea: " + str(self.row))
def execute(self, environment): exp = self.exp.execute(environment) operator = self.operator if exp.type != exp.TYPE.NUMBER: exp.list_errors.append( "Error: 42883: la operacion no existe entre: " + str(operator) + " " + str(exp.type) + "\n En la linea: " + str(self.row)) return ArithmeticError if operator == "+": value = exp.value elif operator == "-": value = exp.value * -1 else: exp.list_errors.append( "Error: 42883: la operacion no existe entre: " + str(operator) + " " + str(exp.type) + "\n En la linea: " + str(self.row)) raise Exception return primitive.Primitive(exp.TYPE.NUMBER, value, self.temp, self.row, self.column)
def execute(self, environment): try: if self.val == "CURRENT_DATE": value = datetime.now().strftime("%Y/%m/%d") elif self.val == "CURRENT_TIME": value = datetime.now().strftime("%H:%M:%S") elif self.val == "TIMESTAMP": if self.optStr == "now": value = datetime.now().strftime("%Y/%m/%d %H:%M:%S") else: value = self.optStr else: # ERROR list_errors.append( "Error: 22007: Formato de fecha invalido " + str(self.str) ) value = self.val return primitive.Primitive(exp.TYPE.STRING, value, self.temp, self.row, self.column) except: list_errors.append("Error: P0001: Error en expresiones de fechas") pass
def execute(self, environment): exp1 = self.exp1.execute(environment) exp2 = self.exp2.execute(environment) operator = self.operator try: if exp1.type != exp.TYPE.BOOLEAN or exp2.type != exp.TYPE.BOOLEAN: raise Exception if isinstance(exp1.value, pd.core.series.Series) or isinstance( exp2.value, pd.core.series.Series ): if operator == "AND": value = exp1.value & exp2.value elif operator == "OR": value = exp1.value | exp2.value else: raise Exception else: if operator == "AND": value = exp1.value and exp2.value elif operator == "OR": value = exp1.value or exp2.value else: raise Exception return primitive.Primitive( exp.TYPE.BOOLEAN, value, self.temp, self.row, self.column ) except: exp.list_errors.append( "Error: 42883: la operacion no existe entre: " + str(exp1.type) + " " + str(operator) + " " + str(exp2.type) + "\n En la linea: " + str(self.row) )
def execute(self, environment): countGr = environment.groupCols if countGr == 0: if self.colData != "*": c = self.colData.execute(environment).value if self.func == "sum": newDf = c.sum() elif self.func == "count": newDf = c.count() elif self.func == "prom": newDf = c.mean() else: newDf = None list_errors.append("Error: 42725: Error en la funcion " + str(self.func) + "\n En la linea: " + str(self.row)) else: c = environment.dataFrame.iloc[:, -1:] if self.func == "count": newDf = len(c) else: newDf = None list_errors.append("Error: 42725: Error en la funcion " + str(self.func) + "\n En la linea: " + str(self.row)) return primitive.Primitive(exp.TYPE.NUMBER, newDf, self.temp, self.row, self.column) if self.colData != "*": # Obtiene las ultimas columnas metidas (Las del group by) df = environment.dataFrame.iloc[:, -countGr:] c = self.colData.execute(environment) x = c.value x = pd.DataFrame(x) x.rename(columns={x.columns[0]: c.temp}, inplace=True) if len(list(x.columns)) > 1: df = pd.concat([df, x.iloc[:, :1]], axis=1) else: df = pd.concat([df, x], axis=1) cols = list(df.columns)[:-1] if self.func == "sum": newDf = df.groupby(cols).sum().reset_index() elif self.func == "count": newDf = df.groupby(cols).count().reset_index() elif self.func == "prom": newDf = df.groupby(cols).mean().reset_index() else: newDf = None list_errors.append("Error: 42725: Error en la funcion " + str(self.func) + "\n En la linea: " + str(self.row)) value = newDf.iloc[:, -1:] else: # Obtiene las ultimas columnas metidas (Las del group by) df = environment.dataFrame.iloc[:, -countGr:] x = df.iloc[:, -1:] x = pd.DataFrame(x) x.rename(columns={x.columns[0]: "count(*)"}, inplace=True) df = pd.concat([df, x], axis=1) cols = list(df.columns)[:-1] if self.func == "count": newDf = df.groupby(cols).count().reset_index() else: newDf = None list_errors.append("Error: 42725: Error en la funcion " + str(self.func) + "\n En la linea: " + str(self.row)) value = newDf.iloc[:, -1:] return primitive.Primitive(exp.TYPE.NUMBER, value, self.temp, self.row, self.column)
def Primitive(type_, value, temp, row, column): return primitive.Primitive(type_, value, temp, row, column)
def execute(self, environment): try: if self.type == "TIMESTAMP": if self.str[0] == "now": self.str = datetime.now().strftime( "%Y/%m/%d %H:%M:%S").split() if self.opt == "YEAR": val = self.str[0][:4] elif self.opt == "MONTH": val = self.str[0][5:7] elif self.opt == "DAY": val = self.str[0][8:10] elif self.opt == "HOUR": val = self.str[1][:2] elif self.opt == "MINUTE": val = self.str[1][3:5] elif self.opt == "SECOND": val = self.str[1][6:8] else: val = self.str raise Exception elif self.type == "DATE": if self.opt == "YEAR": val = self.str[0][:4] elif self.opt == "MONTH": val = self.str[0][5:7] elif self.opt == "DAY": val = self.str[0][8:10] else: val = self.str raise Exception elif self.type == "TIME": if self.opt == "HOUR": val = self.str[0][:2] elif self.opt == "MINUTE": val = self.str[0][3:5] elif self.opt == "SECOND": val = self.str[0][6:8] else: val = self.str raise Exception elif self.type == "INTERVAL": if self.opt == "YEAR": idx = self.str.index("years") val = self.str[idx - 1] elif self.opt == "MONTH": idx = self.str.index("months") val = self.str[idx - 1] elif self.opt == "DAY": idx = self.str.index("days") val = self.str[idx - 1] elif self.opt == "HOUR": idx = self.str.index("hours") val = self.str[idx - 1] elif self.opt == "MINUTE": idx = self.str.index("minutes") val = self.str[idx - 1] elif self.opt == "SECOND": idx = self.str.index("seconds") val = self.str[idx - 1] else: val = self.str raise Exception else: val = self.str raise Exception return primitive.Primitive(exp.TYPE.NUMBER, int(val), self.temp, self.row, self.column) except TypeError: raise exp.list_errors.append( "Error: 42804: discrepancia de tipo de datos ") except ValueError: # cuando no tiene el valor INTERVAL raise exp.list_errors.append( "Error: 22007:sintaxis de entrada no válida para el tipo 'interval' " ) except: raise exp.list_errors.append( "Error: 22007: Formato de fecha invalido " + str(self.str))
def execute(self, environment): try: valores = self.colData.execute(environment) if isinstance(valores.value, pd.core.series.Series): lst = valores.value.tolist() lst = [v.split() for v in lst] else: lst = [valores.split()] if valores.type == exp.TYPE.TIMESTAMP or valores.type == exp.TYPE.DATETIME: if self.opt == "YEAR": val = [date[0][:4] for date in lst] elif self.opt == "MONTH": val = [date[0][5:7] for date in lst] elif self.opt == "DAY": val = [date[0][8:10] for date in lst] elif self.opt == "HOUR": val = [date[1][:2] for date in lst] elif self.opt == "MINUTE": val = [date[1][3:5] for date in lst] elif self.opt == "SECOND": val = [date[1][6:8] for date in lst] else: # ERROR exp.list_errors.append( "Error: 22007: Formato de fecha invalido " + str(self.str)) val = self.str elif valores.type == exp.TYPE.DATE: if self.opt == "YEAR": val = [date[0][:4] for date in lst] elif self.opt == "MONTH": val = [date[0][5:7] for date in lst] elif self.opt == "DAY": val = [date[0][8:10] for date in lst] else: val = self.str raise Exception elif valores.type == exp.TYPE.TIME: if self.opt == "HOUR": val = [date[0][:2] for date in lst] elif self.opt == "MINUTE": val = [date[0][3:5] for date in lst] elif self.opt == "SECOND": val = [date[0][6:8] for date in lst] else: val = self.str raise Exception else: val = self.str raise Exception if isinstance(val, list): if len(val) <= 1: val = val[0] else: val = pd.Series(val) return primitive.Primitive(exp.TYPE.NUMBER, val, self.temp, self.row, self.column) except TypeError: exp.list_errors.append( "Error: 42804: discrepancia de tipo de datos ") except ValueError: # cuando no tiene el valor INTERVAL exp.list_errors.append( "Error: 22007:sintaxis de entrada no válida para el tipo 'interval' " ) except: raise exp.list_errors.append( "Error: 22007: Formato de fecha invalido " + str(self.str))
def execute(self, environment): type_ = exp.TYPE.NUMBER try: valores = [] types = [] for p in self.params: obj = p.execute(environment) val = obj.value t = obj.type if isinstance(val, pd.core.series.Series): val = val.tolist() valores.append(val) types.append(t) # Se toma en cuenta que las funcines matematicas # y trigonometricas producen un tipo NUMBER type_ = exp.TYPE.NUMBER if self.function == "abs": value = mf.absolute(*valores) elif self.function == "cbrt": value = mf.cbrt(*valores) elif self.function == "ceil": value = mf.ceil(*valores) elif self.function == "ceiling": value = mf.ceiling(*valores) elif self.function == "degrees": value = mf.degrees(*valores) elif self.function == "div": value = mf.div(*valores) elif self.function == "exp": value = mf.exp(*valores) elif self.function == "factorial": value = mf.factorial(*valores) elif self.function == "floor": value = mf.floor(*valores) elif self.function == "gcd": value = mf.gcd(*valores) elif self.function == "lcm": value = mf.lcm(*valores) elif self.function == "ln": value = mf.ln(*valores) elif self.function == "log": value = mf.log(*valores) elif self.function == "log10": value = mf.log10(*valores) elif self.function == "mod": value = mf.mod(*valores) elif self.function == "pi": value = mf.pi() elif self.function == "power": value = mf.pow(*valores) elif self.function == "radians": value = mf.radians(*valores) elif self.function == "round": value = mf.round(*valores) elif self.function == "sign": value = mf.sign(*valores) elif self.function == "sqrt": value = mf.sqrt(*valores) elif self.function == "trunc": value = mf.truncate_col(*valores) elif self.function == "width_bucket": value = mf.with_bucket(*valores) elif self.function == "random": value = mf.random_() elif self.function == "acos": value = trf.acos(*valores) elif self.function == "acosd": value = trf.acosd(*valores) elif self.function == "asin": value = trf.asin(*valores) elif self.function == "asind": value = trf.asind(*valores) elif self.function == "atan": value = trf.atan(*valores) elif self.function == "atand": value = trf.atand(*valores) elif self.function == "atan2": value = trf.atan2(*valores) elif self.function == "atan2d": value = trf.atan2d(*valores) elif self.function == "cos": value = trf.cos(*valores) elif self.function == "cosd": value = trf.cosd(*valores) elif self.function == "cot": value = trf.cot(*valores) elif self.function == "cotd": value = trf.cotd(*valores) elif self.function == "sin": value = trf.sin(*valores) elif self.function == "sind": value = trf.sind(*valores) elif self.function == "tan": value = trf.tan(*valores) elif self.function == "tand": value = trf.tand(*valores) elif self.function == "sinh": value = trf.sinh(*valores) elif self.function == "cosh": value = trf.cosh(*valores) elif self.function == "tanh": value = trf.tanh(*valores) elif self.function == "asinh": value = trf.asinh(*valores) elif self.function == "acosh": value = trf.acosh(*valores) elif self.function == "atanh": value = trf.atanh(*valores) elif self.function == "length": value = strf.lenght(*valores) elif self.function == "substring": type_ = exp.TYPE.STRING value = strf.substring(*valores) elif self.function == "trim": type_ = exp.TYPE.STRING value = strf.trim_(*valores) elif self.function == "get_byte": value = strf.get_byte(*valores) elif self.function == "md5": type_ = exp.TYPE.STRING value = strf.md5(*valores) elif self.function == "set_byte": type_ = exp.TYPE.STRING value = strf.set_byte(*valores) elif self.function == "sha256": type_ = exp.TYPE.STRING value = strf.sha256(*valores) elif self.function == "substr": type_ = exp.TYPE.STRING value = strf.substring(*valores) elif self.function == "convert_date": type_ = exp.TYPE.DATETIME value = strf.convert_date(*valores) elif self.function == "convert_int": value = strf.convert_int(*valores) elif self.function == "encode": type_ = exp.TYPE.STRING value = strf.encode(*valores) elif self.function == "decode": type_ = exp.TYPE.STRING value = strf.decode(*valores) # Se toma en cuenta que la funcion now produce tipo DATE elif self.function == "now": type_ = exp.TYPE.DATETIME value = datetime.now().strftime("%Y/%m/%d %H:%M:%S") else: # TODO: Agregar un error de funcion desconocida value = valores[0] if isinstance(value, list): if len(value) <= 1: value = value[0] else: value = pd.Series(value) return primitive.Primitive(type_, value, self.temp, self.row, self.column) except TypeError: list_errors.append("Error: 42883: La funcion " + str(self.function) + "(" + str(type_) + ") no existe" + "\n En la linea: " + str(self.row)) except: list_errors.append("Error: P0001: Error en funciones")