def reduce_MINUS_Expr(self, *kids): arg = kids[1].val if isinstance(arg, qlast.BaseRealConstant): # Special case for -<real_const> so that type inference based # on literal size works correctly in the case of INT_MIN and # friends. self.val = type(arg)(value=arg.value, is_negative=True) else: self.val = qlast.UnaryOp(op=kids[0].val, operand=arg)
def visit_OperatorCall(self, node): args = node.args if node.operator_kind is ft.OperatorKind.INFIX: result = qlast.BinOp( left=self.visit(args[0]), right=self.visit(args[1]), op=node.func_shortname.name, ) elif node.operator_kind is ft.OperatorKind.PREFIX: result = qlast.UnaryOp( operand=self.visit(args[0]), op=node.func_shortname.name, ) else: raise RuntimeError( f'unexpected operator kind: {node.operator_kind}') return result
def visit_FunctionCall(self, node): # FIXME: this is a temporary solution to bridge the gap to EdgeQL if node.agg_set_modifier == qlast.AggDISTINCT: args = qlast.UnaryOp(op=qlast.DISTINCT, operand=node.args[0]) else: args = node.args # FIXME: hack to reconstruct args for a trivial aggregate function args = [qlast.FuncArg(arg=arg) for arg in self.visit(args)] if node.agg_filter or node.agg_sort: args[0].sort = node.agg_sort args[0].filter = (self.visit(node.agg_filter) if node.agg_filter is not None else None) result = qlast.FunctionCall( func=(node.func.shortname.module, node.func.shortname.name), args=args, ) return result
def visit_ObjectField(self, node): fname = node.name # handle boolean ops if fname == 'and': return self._visit_list_of_inputs(node.value.value, ast.ops.AND) elif fname == 'or': return self._visit_list_of_inputs(node.value.value, ast.ops.OR) elif fname == 'not': return qlast.UnaryOp(op=ast.ops.NOT, operand=self.visit(node.value)) # handle various scalar ops op = gt.GQL_TO_OPS_MAP.get(fname) if op: value = self.visit(node.value) return qlast.BinOp(left=self._context.filter, op=op, right=value) # we're at the beginning of a scalar op _, target = self._get_parent_and_current_type() name = self.get_path_prefix() name.append(qlast.Ptr(ptr=qlast.ObjectRef(name=fname))) name = qlast.Path(steps=name) # potentially need to cast the 'name' side into a <str>, so as # to be compatible with the 'value' typename = target.get_field_type(fname).short_name if (typename != 'str' and gt.EDB_TO_GQL_SCALARS_MAP[typename] in {GraphQLString, GraphQLID}): name = qlast.TypeCast( expr=name, type=qlast.TypeName(maintype=qlast.ObjectRef(name='str')), ) self._context.filter = name return self.visit(node.value)
def reduce_NOT_Expr(self, *kids): self.val = qlast.UnaryOp(op=kids[0].val.upper(), operand=kids[1].val)
def reduce_PLUS_Expr(self, *kids): self.val = qlast.UnaryOp(op=kids[0].val, operand=kids[1].val)
def reduce_DISTINCT_Expr(self, *kids): self.val = qlast.UnaryOp(op='DISTINCT', operand=kids[1].val)
def reduce_EXISTS_Expr(self, *kids): self.val = qlast.UnaryOp(op='EXISTS', operand=kids[1].val)
def visit_DistinctOp(self, node): result = qlast.UnaryOp() result.operand = self.visit(node.expr) result.op = qlast.DISTINCT return result
def visit_UnaryOp(self, node): result = qlast.UnaryOp() result.operand = self.visit(node.expr) result.op = node.op return result
def reduce_NOT_Expr(self, *kids): self.val = qlast.UnaryOp(op=ast.ops.NOT, operand=kids[1].val)
def reduce_MINUS_Expr(self, *kids): self.val = qlast.UnaryOp(op=ast.ops.UMINUS, operand=kids[1].val)