def Val_is_null(context, builder, typ, value): v = Struct(context, builder, value=value) is_null = _get_is_null(builder, v) return is_null
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)
def lower_return_type(context, builder, ty, val): """ Convert value to fit ABI requirement """ if ty == BooleanVal: # Pack structure into int16 # Endian specific iv = BooleanValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(16)) upper = builder.zext(iv.val, lc.Type.int(16)) asint16 = builder.shl(upper, lc.Constant.int(lc.Type.int(16), 8)) asint16 = builder.or_(asint16, lower) return asint16 elif ty == TinyIntVal: # Pack structure into int16 # Endian specific iv = TinyIntValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(16)) upper = builder.zext(iv.val, lc.Type.int(16)) asint16 = builder.shl(upper, lc.Constant.int(lc.Type.int(16), 8)) asint16 = builder.or_(asint16, lower) return asint16 elif ty == SmallIntVal: # Pack structure into int32 # Endian specific iv = SmallIntValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(32)) upper = builder.zext(iv.val, lc.Type.int(32)) asint32 = builder.shl(upper, lc.Constant.int(lc.Type.int(32), 16)) asint32 = builder.or_(asint32, lower) return asint32 elif ty == IntVal: # Pack structure into int64 # Endian specific iv = IntValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(64)) upper = builder.zext(iv.val, lc.Type.int(64)) asint64 = builder.shl(upper, lc.Constant.int(lc.Type.int(64), 32)) asint64 = builder.or_(asint64, lower) return asint64 elif ty == BigIntVal: # Pack structure into { int8, int64 } # Endian specific iv = BigIntValStruct(context, builder, value=val) is_null = builder.zext(_get_is_null(builder, iv), lc.Type.int(8)) asstructi8i64 = builder.insert_value( lc.Constant.undef(lc.Type.struct([lc.Type.int(8), lc.Type.int(64)])), is_null, 0) asstructi8i64 = builder.insert_value(asstructi8i64, iv.val, 1) return asstructi8i64 elif ty == FloatVal: # Pack structure into int64 # Endian specific iv = FloatValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(64)) asint32 = builder.bitcast(iv.val, lc.Type.int(32)) upper = builder.zext(asint32, lc.Type.int(64)) asint64 = builder.shl(upper, lc.Constant.int(lc.Type.int(64), 32)) asint64 = builder.or_(asint64, lower) return asint64 elif ty == DoubleVal: # Pack structure into { int8, double } # Endian specific iv = DoubleValStruct(context, builder, value=val) is_null = builder.zext(_get_is_null(builder, iv), lc.Type.int(8)) asstructi8double = builder.insert_value( lc.Constant.undef( lc.Type.struct([lc.Type.int(8), lc.Type.double()])), is_null, 0) asstructi8double = builder.insert_value(asstructi8double, iv.val, 1) return asstructi8double elif ty == StringVal: # Pack structure into { int64, int8* } # Endian specific iv = StringValStruct(context, builder, value=val) is_null = builder.zext(_get_is_null(builder, iv), lc.Type.int(64)) len_ = builder.zext(iv.len, lc.Type.int(64)) asint64 = builder.shl(len_, lc.Constant.int(lc.Type.int(64), 32)) asint64 = builder.or_(asint64, is_null) asstructi64i8p = builder.insert_value( lc.Constant.undef( lc.Type.struct( [lc.Type.int(64), lc.Type.pointer(lc.Type.int(8))])), asint64, 0) asstructi64i8p = builder.insert_value(asstructi64i8p, iv.ptr, 1) return asstructi64i8p else: return val
def lower_return_type(context, builder, ty, val): """ Convert value to fit ABI requirement """ if ty == BooleanVal: # Pack structure into int16 # Endian specific iv = BooleanValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(16)) upper = builder.zext(iv.val, lc.Type.int(16)) asint16 = builder.shl(upper, lc.Constant.int(lc.Type.int(16), 8)) asint16 = builder.or_(asint16, lower) return asint16 elif ty == TinyIntVal: # Pack structure into int16 # Endian specific iv = TinyIntValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(16)) upper = builder.zext(iv.val, lc.Type.int(16)) asint16 = builder.shl(upper, lc.Constant.int(lc.Type.int(16), 8)) asint16 = builder.or_(asint16, lower) return asint16 elif ty == SmallIntVal: # Pack structure into int32 # Endian specific iv = SmallIntValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(32)) upper = builder.zext(iv.val, lc.Type.int(32)) asint32 = builder.shl(upper, lc.Constant.int(lc.Type.int(32), 16)) asint32 = builder.or_(asint32, lower) return asint32 elif ty == IntVal: # Pack structure into int64 # Endian specific iv = IntValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(64)) upper = builder.zext(iv.val, lc.Type.int(64)) asint64 = builder.shl(upper, lc.Constant.int(lc.Type.int(64), 32)) asint64 = builder.or_(asint64, lower) return asint64 elif ty == BigIntVal: # Pack structure into { int8, int64 } # Endian specific iv = BigIntValStruct(context, builder, value=val) is_null = builder.zext(_get_is_null(builder, iv), lc.Type.int(8)) asstructi8i64 = builder.insert_value(lc.Constant.undef( lc.Type.struct([lc.Type.int(8), lc.Type.int(64)])), is_null, 0) asstructi8i64 = builder.insert_value(asstructi8i64, iv.val, 1) return asstructi8i64 elif ty == FloatVal: # Pack structure into int64 # Endian specific iv = FloatValStruct(context, builder, value=val) lower = builder.zext(_get_is_null(builder, iv), lc.Type.int(64)) asint32 = builder.bitcast(iv.val, lc.Type.int(32)) upper = builder.zext(asint32, lc.Type.int(64)) asint64 = builder.shl(upper, lc.Constant.int(lc.Type.int(64), 32)) asint64 = builder.or_(asint64, lower) return asint64 elif ty == DoubleVal: # Pack structure into { int8, double } # Endian specific iv = DoubleValStruct(context, builder, value=val) is_null = builder.zext(_get_is_null(builder, iv), lc.Type.int(8)) asstructi8double = builder.insert_value(lc.Constant.undef( lc.Type.struct([lc.Type.int(8), lc.Type.double()])), is_null, 0) asstructi8double = builder.insert_value(asstructi8double, iv.val, 1) return asstructi8double elif ty == StringVal: # Pack structure into { int64, int8* } # Endian specific iv = StringValStruct(context, builder, value=val) is_null = builder.zext(_get_is_null(builder, iv), lc.Type.int(64)) len_ = builder.zext(iv.len, lc.Type.int(64)) asint64 = builder.shl(len_, lc.Constant.int(lc.Type.int(64), 32)) asint64 = builder.or_(asint64, is_null) asstructi64i8p = builder.insert_value(lc.Constant.undef(lc.Type.struct( [lc.Type.int(64), lc.Type.pointer(lc.Type.int(8))])), asint64, 0) asstructi64i8p = builder.insert_value(asstructi64i8p, iv.ptr, 1) return asstructi64i8p else: return val
def Val_is_null(context, builder, typ, value): v = Struct(context, builder, value=value) is_null = _get_is_null(builder, v) return is_null
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)