Beispiel #1
0
 def get_constant_struct(self, builder, ty, val):
     # override for converting literals to *Vals, incl. None
     if ty in self._impala_types and val is None:
         iv = TYPE_LAYOUT[ty](self, builder)
         _set_is_null(builder, iv, cgutils.true_bit)
         return iv._getvalue()
     elif ty == BooleanVal:
         const = lc.Constant.int(lc.Type.int(8), val)
         return BooleanVal_ctor(self, builder, None, [const])
     elif ty == TinyIntVal:
         const = lc.Constant.int(lc.Type.int(8), val)
         return TinyIntVal_ctor(self, builder, None, [const])
     elif ty == SmallIntVal:
         const = lc.Constant.int(lc.Type.int(16), val)
         return SmallIntVal_ctor(self, builder, None, [const])
     elif ty == IntVal:
         const = lc.Constant.int(lc.Type.int(32), val)
         return IntVal_ctor(self, builder, None, [const])
     elif ty == BigIntVal:
         const = lc.Constant.int(lc.Type.int(64), val)
         return BigIntVal_ctor(self, builder, None, [const])
     elif ty == FloatVal:
         const = lc.Constant.real(lc.Type.float(), val)
         return FloatVal_ctor(self, builder, None, [const])
     elif ty == DoubleVal:
         const = lc.Constant.real(lc.Type.double(), val)
         return DoubleVal_ctor(self, builder, None, [const])
     elif ty == StringVal:
         iv = StringValStruct(self, builder)
         _set_is_null(builder, iv, cgutils.false_bit)
         iv.len = lc.Constant.int(lc.Type.int(32), len(val))
         iv.ptr = self.get_constant_string(builder, ntypes.string, val)
         return iv._getvalue()
     else:
         return super(ImpalaTargetContext, self).get_constant_struct(builder, ty, val)
Beispiel #2
0
def StringVal_ctor(context, builder, sig, args):
    """StringVal(ntypes.string)"""
    [x] = args
    iv = StringValStruct(context, builder)
    _set_is_null(builder, iv, cgutils.false_bit)
    fndesc = lowering.ExternalFunctionDescriptor('strlen', ntypes.uintp,
            [ntypes.CPointer(ntypes.char)])
    func = context.declare_external_function(cgutils.get_module(builder), fndesc)
    strlen_x = context.call_external_function(builder, func, fndesc.argtypes, [x])
    len_x = builder.trunc(strlen_x, lc.Type.int(32))
    iv.len = len_x
    iv.ptr = x
    return iv._getvalue()
Beispiel #3
0
def StringVal_ctor(context, builder, sig, args):
    """StringVal(ntypes.string)"""
    [x] = args
    iv = StringValStruct(context, builder)
    _set_is_null(builder, iv, cgutils.false_bit)
    fndesc = lowering.ExternalFunctionDescriptor(
        'strlen', ntypes.uintp, [ntypes.CPointer(ntypes.char)])
    func = context.declare_external_function(cgutils.get_module(builder),
                                             fndesc)
    strlen_x = context.call_external_function(builder, func, fndesc.argtypes,
                                              [x])
    len_x = builder.trunc(strlen_x, lc.Type.int(32))
    iv.len = len_x
    iv.ptr = x
    return iv._getvalue()
Beispiel #4
0
 def get_constant_struct(self, builder, ty, val):
     # override for converting literals to *Vals, incl. None
     if ty in self._impala_types and val is None:
         iv = TYPE_LAYOUT[ty](self, builder)
         _set_is_null(builder, iv, cgutils.true_bit)
         return iv._getvalue()
     elif ty == BooleanVal:
         const = lc.Constant.int(lc.Type.int(8), val)
         return BooleanVal_ctor(self, builder, None, [const])
     elif ty == TinyIntVal:
         const = lc.Constant.int(lc.Type.int(8), val)
         return TinyIntVal_ctor(self, builder, None, [const])
     elif ty == SmallIntVal:
         const = lc.Constant.int(lc.Type.int(16), val)
         return SmallIntVal_ctor(self, builder, None, [const])
     elif ty == IntVal:
         const = lc.Constant.int(lc.Type.int(32), val)
         return IntVal_ctor(self, builder, None, [const])
     elif ty == BigIntVal:
         const = lc.Constant.int(lc.Type.int(64), val)
         return BigIntVal_ctor(self, builder, None, [const])
     elif ty == FloatVal:
         const = lc.Constant.real(lc.Type.float(), val)
         return FloatVal_ctor(self, builder, None, [const])
     elif ty == DoubleVal:
         const = lc.Constant.real(lc.Type.double(), val)
         return DoubleVal_ctor(self, builder, None, [const])
     elif ty == StringVal:
         iv = StringValStruct(self, builder)
         _set_is_null(builder, iv, cgutils.false_bit)
         iv.len = lc.Constant.int(lc.Type.int(32), len(val))
         iv.ptr = self.get_constant_string(builder, ntypes.string, val)
         return iv._getvalue()
     else:
         return super(ImpalaTargetContext,
                      self).get_constant_struct(builder, ty, val)
Beispiel #5
0
 def Val_ctor(context, builder, sig, args):
     [x] = args
     v = Struct(context, builder)
     _set_is_null(builder, v, cgutils.false_bit)
     v.val = x
     return v._getvalue()
Beispiel #6
0
    def cast(self, builder, val, fromty, toty):
        if fromty not in self._impala_types and toty not in self._impala_types:
            return super(ImpalaTargetContext, self).cast(builder, val, fromty, toty)

        if fromty == toty:
            return val

        # handle NULLs and Nones
        if fromty == ntypes.none and toty in self._impala_types:
            iv = TYPE_LAYOUT[toty](self, builder)
            _set_is_null(builder, iv, cgutils.true_bit)
            return iv._getvalue()
        if fromty in self._impala_types and toty == AnyVal:
            iv1 = TYPE_LAYOUT[fromty](self, builder, value=val)
            is_null = _get_is_null(builder, iv1)
            iv2 = AnyValStruct(self, builder)
            # this is equiv to _set_is_null, but changes the GEP bc of AnyVal's structure
            byte = builder.zext(is_null, lc.Type.int(8))
            builder.store(byte, builder.gep(iv2._getpointer(),
                    [lc.Constant.int(lc.Type.int(32), 0)] * 2, inbounds=True))
            return iv2._getvalue()

        if fromty == BooleanVal:
            v = BooleanValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.boolean, toty)
        if fromty == TinyIntVal:
            v = TinyIntValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.int8, toty)
        if fromty == SmallIntVal:
            v = SmallIntValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.int16, toty)
        if fromty == IntVal:
            v = IntValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.int32, toty)
        if fromty == BigIntVal:
            v = BigIntValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.int64, toty)
        if fromty == FloatVal:
            v = FloatValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.float32, toty)
        if fromty == DoubleVal:
            v = DoubleValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.float64, toty)

        # no way fromty is a *Val starting here
        if toty == BooleanVal:
            val = super(ImpalaTargetContext, self).cast(builder, val, fromty, ntypes.int8)
            return BooleanVal_ctor(self, builder, None, [val])
        if toty == TinyIntVal:
            val = super(ImpalaTargetContext, self).cast(builder, val, fromty, ntypes.int8)
            return TinyIntVal_ctor(self, builder, None, [val])
        if toty == SmallIntVal:
            val = super(ImpalaTargetContext, self).cast(builder, val, fromty, ntypes.int16)
            return SmallIntVal_ctor(self, builder, None, [val])
        if toty == IntVal:
            val = super(ImpalaTargetContext, self).cast(builder, val, fromty, ntypes.int32)
            return IntVal_ctor(self, builder, None, [val])
        if toty == BigIntVal:
            val = super(ImpalaTargetContext, self).cast(builder, val, fromty, ntypes.int64)
            return BigIntVal_ctor(self, builder, None, [val])
        if toty == FloatVal:
            val = super(ImpalaTargetContext, self).cast(builder, val, fromty, ntypes.float32)
            return FloatVal_ctor(self, builder, None, [val])
        if toty == DoubleVal:
            val = super(ImpalaTargetContext, self).cast(builder, val, fromty, ntypes.float64)
            return DoubleVal_ctor(self, builder, None, [val])
        if toty == StringVal:
            return StringVal_ctor(self, builder, None, [val])

        return super(ImpalaTargetContext, self).cast(builder, val, fromty, toty)
Beispiel #7
0
def raise_return_type(context, builder, ty, val):
    if ty == BooleanVal:
        bv = BooleanValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, bv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(16), 8))
        bv.val = builder.trunc(shifted, lc.Type.int(8))
        return bv._getvalue()
    elif ty == TinyIntVal:
        tiv = TinyIntValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, tiv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(16), 8))
        tiv.val = builder.trunc(shifted, lc.Type.int(8))
        return tiv._getvalue()
    elif ty == SmallIntVal:
        siv = SmallIntValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, siv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(32), 16))
        siv.val = builder.trunc(shifted, lc.Type.int(16))
        return siv._getvalue()
    elif ty == IntVal:
        iv = IntValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, iv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(64), 32))
        iv.val = builder.trunc(shifted, lc.Type.int(32))
        return iv._getvalue()
    elif ty == BigIntVal:
        biv = BigIntValStruct(context, builder)
        is_null = builder.extract_value(val, 0)
        _set_is_null(builder, biv, is_null)
        biv.val = builder.extract_value(val, 1)
        return biv._getvalue()
    elif ty == FloatVal:
        fv = FloatValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, fv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(64), 32))
        truncated = builder.trunc(shifted, lc.Type.int(32))
        fv.val = builder.bitcast(truncated, lc.Type.float())
        return fv._getvalue()
    elif ty == DoubleVal:
        dv = DoubleValStruct(context, builder)
        is_null = builder.extract_value(val, 0)
        _set_is_null(builder, dv, is_null)
        dv.val = builder.extract_value(val, 1)
        return dv._getvalue()
    elif ty == StringVal:
        sv = StringValStruct(context, builder)
        packed = builder.extract_value(val, 0)
        is_null = builder.trunc(packed, lc.Type.int(8))
        _set_is_null(builder, sv, is_null)
        shifted = builder.lshr(packed, lc.Constant.int(lc.Type.int(64), 32))
        sv.len = builder.trunc(shifted, lc.Type.int(32))
        sv.ptr = builder.extract_value(val, 1)
        return sv._getvalue()
    else:
        return val
Beispiel #8
0
def raise_return_type(context, builder, ty, val):
    if ty == BooleanVal:
        bv = BooleanValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, bv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(16), 8))
        bv.val = builder.trunc(shifted, lc.Type.int(8))
        return bv._getvalue()
    elif ty == TinyIntVal:
        tiv = TinyIntValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, tiv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(16), 8))
        tiv.val = builder.trunc(shifted, lc.Type.int(8))
        return tiv._getvalue()
    elif ty == SmallIntVal:
        siv = SmallIntValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, siv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(32), 16))
        siv.val = builder.trunc(shifted, lc.Type.int(16))
        return siv._getvalue()
    elif ty == IntVal:
        iv = IntValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, iv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(64), 32))
        iv.val = builder.trunc(shifted, lc.Type.int(32))
        return iv._getvalue()
    elif ty == BigIntVal:
        biv = BigIntValStruct(context, builder)
        is_null = builder.extract_value(val, 0)
        _set_is_null(builder, biv, is_null)
        biv.val = builder.extract_value(val, 1)
        return biv._getvalue()
    elif ty == FloatVal:
        fv = FloatValStruct(context, builder)
        is_null = builder.trunc(val, lc.Type.int(8))
        _set_is_null(builder, fv, is_null)
        shifted = builder.lshr(val, lc.Constant.int(lc.Type.int(64), 32))
        truncated = builder.trunc(shifted, lc.Type.int(32))
        fv.val = builder.bitcast(truncated, lc.Type.float())
        return fv._getvalue()
    elif ty == DoubleVal:
        dv = DoubleValStruct(context, builder)
        is_null = builder.extract_value(val, 0)
        _set_is_null(builder, dv, is_null)
        dv.val = builder.extract_value(val, 1)
        return dv._getvalue()
    elif ty == StringVal:
        sv = StringValStruct(context, builder)
        packed = builder.extract_value(val, 0)
        is_null = builder.trunc(packed, lc.Type.int(8))
        _set_is_null(builder, sv, is_null)
        shifted = builder.lshr(packed, lc.Constant.int(lc.Type.int(64), 32))
        sv.len = builder.trunc(shifted, lc.Type.int(32))
        sv.ptr = builder.extract_value(val, 1)
        return sv._getvalue()
    else:
        return val
Beispiel #9
0
 def Val_ctor(context, builder, sig, args):
     [x] = args
     v = Struct(context, builder)
     _set_is_null(builder, v, cgutils.false_bit)
     v.val = x
     return v._getvalue()
Beispiel #10
0
    def cast(self, builder, val, fromty, toty):
        if fromty not in self._impala_types and toty not in self._impala_types:
            return super(ImpalaTargetContext,
                         self).cast(builder, val, fromty, toty)

        if fromty == toty:
            return val

        # handle NULLs and Nones
        if fromty == ntypes.none and toty in self._impala_types:
            iv = TYPE_LAYOUT[toty](self, builder)
            _set_is_null(builder, iv, cgutils.true_bit)
            return iv._getvalue()
        if fromty in self._impala_types and toty == AnyVal:
            iv1 = TYPE_LAYOUT[fromty](self, builder, value=val)
            is_null = _get_is_null(builder, iv1)
            iv2 = AnyValStruct(self, builder)
            # this is equiv to _set_is_null, but changes the GEP bc of AnyVal's
            # structure
            byte = builder.zext(is_null, lc.Type.int(8))
            builder.store(
                byte,
                builder.gep(iv2._getpointer(),
                            [lc.Constant.int(lc.Type.int(32), 0)] * 2,
                            inbounds=True))
            return iv2._getvalue()

        if fromty == BooleanVal:
            v = BooleanValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.boolean, toty)
        if fromty == TinyIntVal:
            v = TinyIntValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.int8, toty)
        if fromty == SmallIntVal:
            v = SmallIntValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.int16, toty)
        if fromty == IntVal:
            v = IntValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.int32, toty)
        if fromty == BigIntVal:
            v = BigIntValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.int64, toty)
        if fromty == FloatVal:
            v = FloatValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.float32, toty)
        if fromty == DoubleVal:
            v = DoubleValStruct(self, builder, val)
            return self.cast(builder, v.val, ntypes.float64, toty)

        # no way fromty is a *Val starting here
        if toty == BooleanVal:
            val = super(ImpalaTargetContext,
                        self).cast(builder, val, fromty, ntypes.int8)
            return BooleanVal_ctor(self, builder, None, [val])
        if toty == TinyIntVal:
            val = super(ImpalaTargetContext,
                        self).cast(builder, val, fromty, ntypes.int8)
            return TinyIntVal_ctor(self, builder, None, [val])
        if toty == SmallIntVal:
            val = super(ImpalaTargetContext,
                        self).cast(builder, val, fromty, ntypes.int16)
            return SmallIntVal_ctor(self, builder, None, [val])
        if toty == IntVal:
            val = super(ImpalaTargetContext,
                        self).cast(builder, val, fromty, ntypes.int32)
            return IntVal_ctor(self, builder, None, [val])
        if toty == BigIntVal:
            val = super(ImpalaTargetContext,
                        self).cast(builder, val, fromty, ntypes.int64)
            return BigIntVal_ctor(self, builder, None, [val])
        if toty == FloatVal:
            val = super(ImpalaTargetContext,
                        self).cast(builder, val, fromty, ntypes.float32)
            return FloatVal_ctor(self, builder, None, [val])
        if toty == DoubleVal:
            val = super(ImpalaTargetContext,
                        self).cast(builder, val, fromty, ntypes.float64)
            return DoubleVal_ctor(self, builder, None, [val])
        if toty == StringVal:
            return StringVal_ctor(self, builder, None, [val])

        return super(ImpalaTargetContext, self).cast(builder, val, fromty,
                                                     toty)