EV-ClickHouse
view release on metacpan or search on metacpan
if (nrows == 0) return 1;
if (ct->inner->code == CT_NULLABLE) {
/* Dict travels as plain T with slot 0 reserved for NULL:
* [T default] + rows (nulls substituted), null->0 else i+1. */
const char **dvals;
size_t *dlens;
uint8_t *nulls;
const char *zp;
size_t zlen;
nullable_inner_zero(ct->inner->inner, &zp, &zlen);
Newx(dvals, nrows + 1, const char *);
Newx(dlens, nrows + 1, size_t);
Newx(nulls, nrows ? nrows : 1, uint8_t);
dvals[0] = zp;
dlens[0] = zlen;
for (i = 0; i < nrows; i++) {
if (is_tsv_null(values[i], value_lens[i])) {
nulls[i] = 1;
dvals[i + 1] = zp;
dlens[i + 1] = zlen;
} else {
nulls[i] = 0;
dvals[i + 1] = values[i];
dlens[i + 1] = value_lens[i];
}
}
lc_write_keys_header(b, nrows + 1, &idx_size);
nbuf_init(&dict_buf);
rc = encode_column_text(&dict_buf, dvals, dlens, nrows + 1,
ct->inner->inner);
Safefree(dvals);
Safefree(dlens);
if (!rc) { Safefree(dict_buf.data); Safefree(nulls); return 0; }
nbuf_append(b, dict_buf.data, dict_buf.len);
Safefree(dict_buf.data);
lc_write_nullable_indices(b, nrows, idx_size, nulls);
Safefree(nulls);
return 1;
}
lc_write_keys_header(b, nrows, &idx_size);
nbuf_init(&dict_buf);
rc = encode_column_text(&dict_buf, values, value_lens, nrows, ct->inner);
if (!rc) { Safefree(dict_buf.data); return 0; }
nbuf_append(b, dict_buf.data, dict_buf.len);
Safefree(dict_buf.data);
lc_write_indices(b, nrows, idx_size);
return 1;
}
default:
return 0; /* unsupported type â fall back to inline SQL */
}
}
/* A plain number never reads as date text; stringifying it would only
* allocate a PV in the caller's scalar. */
#define SV_PLAIN_NUM(sv) (!SvGMAGICAL(sv) && !SvPOK(sv) && !SvROK(sv) \
&& (SvIOK(sv) || SvNOK(sv)))
/* The text SvPV would give a plain number (same rules as sv_2pv: IV
* first, 0 for zeroes, Inf/NaN, %g at NV_DIG), written to buf instead of
* cached in the caller's scalar. buf holds SV_NUM_TEXT_MAX bytes. */
#define SV_NUM_TEXT_MAX 64
#if defined(__SIZEOF_INT128__) && NVSIZE == 8 && NV_DIG == 15
#define HAVE_FAST_G15 1
/* The 15 significant digits %.15g prints for 1e-13 <= |v| < 1e21: the
* exact binary value rounded half-even, as glibc does. v equals
* q * 10^(e10 - 14) after rounding, q in [10^14, 10^15). 0 = out of range. */
static int g15_digits(double v, uint64_t *qp, int *e10p, int *negp) {
static const uint64_t p5[28] = {
1ULL, 5ULL, 25ULL, 125ULL, 625ULL, 3125ULL, 15625ULL, 78125ULL,
390625ULL, 1953125ULL, 9765625ULL, 48828125ULL, 244140625ULL,
1220703125ULL, 6103515625ULL, 30517578125ULL, 152587890625ULL,
762939453125ULL, 3814697265625ULL, 19073486328125ULL,
95367431640625ULL, 476837158203125ULL, 2384185791015625ULL,
11920928955078125ULL, 59604644775390625ULL, 298023223876953125ULL,
1490116119384765625ULL, 7450580596923828125ULL
};
const uint64_t lo15 = 100000000000000ULL, hi15 = 1000000000000000ULL;
uint64_t bits, mant, q;
int exp2, neg, e10, tries;
double a;
memcpy(&bits, &v, 8);
neg = (int)(bits >> 63);
exp2 = (int)((bits >> 52) & 0x7ff);
if (exp2 == 0 || exp2 == 0x7ff) return 0;
a = neg ? -v : v;
if (!(a >= 1e-13 && a < 1e21)) return 0;
mant = (bits & ((1ULL << 52) - 1)) | (1ULL << 52);
exp2 -= 1075;
/* floor(log10(a)) to within one, corrected below */
e10 = ((exp2 + 52) * 78913) >> 18;
/* Double arithmetic first: a * 10^k is off by at most 0.12 from the
* exact product, so it decides the rounding unless it lands near .5
* or a digit-count boundary. */
for (tries = 0; tries < 3; tries++) {
static const double p10d[23] = {
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22
};
int k = 14 - e10;
double y, f, r;
if (k < -22 || k > 22) break;
y = k >= 0 ? a * p10d[k] : a / p10d[-k];
if (y < 1e14) { e10--; continue; }
if (y >= 1e15) { e10++; continue; }
if (y < 1e14 + 1 || y > 1e15 - 2) break;
f = floor(y);
r = y - f;
if (r > 0.3 && r < 0.7) break;
*qp = (uint64_t)f + (r >= 0.7);
*e10p = e10;
*negp = neg;
return 1;
}
for (tries = 0; ; tries++) {
int k = 14 - e10, s, up;
unsigned __int128 n, r;
if (tries > 3 || k < -7 || k > 27) return 0;
if (k >= 0) {
if (q >= hi15) { e10++; continue; }
if (q < lo15) { e10--; continue; }
if (up) q++;
if (q == hi15) { q = lo15; e10++; }
break;
}
*qp = q;
*e10p = e10;
*negp = neg;
return 1;
}
/* snprintf(buf, "%.15g", v) where g15_digits applies, else 0. */
static int fast_g15(double v, char *buf) {
uint64_t q;
int e10, neg;
char dig[16], *p = buf;
if (!g15_digits(v, &q, &e10, &neg)) return 0;
{
int i, nd = 15;
uint32_t hi = (uint32_t)(q / 100000000), lo = (uint32_t)(q % 100000000);
for (i = 14; i >= 7; i--) { dig[i] = (char)('0' + lo % 10); lo /= 10; }
for (i = 6; i >= 0; i--) { dig[i] = (char)('0' + hi % 10); hi /= 10; }
while (nd > 1 && dig[nd - 1] == '0') nd--;
if (neg) *p++ = '-';
if (e10 < -4 || e10 >= 15) {
int x = e10 < 0 ? -e10 : e10;
*p++ = dig[0];
if (nd > 1) { *p++ = '.'; for (i = 1; i < nd; i++) *p++ = dig[i]; }
*p++ = 'e';
*p++ = e10 < 0 ? '-' : '+';
if (x >= 100) *p++ = (char)('0' + x / 100);
*p++ = (char)('0' + x / 10 % 10);
*p++ = (char)('0' + x % 10);
} else if (e10 >= 0) {
for (i = 0; i <= e10; i++) *p++ = i < 15 ? dig[i] : '0';
if (nd > e10 + 1) { *p++ = '.'; for (i = e10 + 1; i < nd; i++) *p++ = dig[i]; }
} else {
*p++ = '0';
*p++ = '.';
for (i = 0; i < -e10 - 1; i++) *p++ = '0';
for (i = 0; i < nd; i++) *p++ = dig[i];
}
}
*p = '\0';
return (int)(p - buf);
}
#endif
static const char *sv_num_text(pTHX_ SV *sv, char *buf, STRLEN *len) {
NV nv;
int n;
if (SvIOK(sv)) {
n = SvIsUV(sv) ? snprintf(buf, SV_NUM_TEXT_MAX, "%" UVuf, SvUVX(sv))
: snprintf(buf, SV_NUM_TEXT_MAX, "%" IVdf, SvIVX(sv));
} else {
nv = SvNVX(sv);
if (nv == 0.0 && !Perl_isnan(nv))
n = snprintf(buf, SV_NUM_TEXT_MAX, "0");
else if (Perl_isnan(nv))
n = snprintf(buf, SV_NUM_TEXT_MAX, "NaN");
else if (Perl_isinf(nv))
n = snprintf(buf, SV_NUM_TEXT_MAX, nv > 0 ? "Inf" : "-Inf");
else {
#ifdef HAVE_FAST_G15
n = fast_g15((double)nv, buf);
if (!n)
#endif
n = snprintf(buf, SV_NUM_TEXT_MAX, "%.*" NVgf, NV_DIG, nv);
}
}
*len = (STRLEN)n;
return buf;
}
/* SvPV(sv, len) without caching a string in a plain number. */
#define SV_TEXT(sv, buf, len) \
(SV_PLAIN_NUM(sv) ? sv_num_text(aTHX_ (sv), (buf), &(len)) : SvPV((sv), (len)))
/* parse_decimal_to_raw() of the text SvPV gives a plain number, computed
* from the number: 1 when done (*ok the verdict), 0 to parse the text. */
static int num_decimal_raw(pTHX_ SV *sv, int scale, int precision,
int64_t *raw, int *ok) {
static const uint64_t p10[19] = {
1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL, 1000000ULL,
10000000ULL, 100000000ULL, 1000000000ULL, 10000000000ULL,
100000000000ULL, 1000000000000ULL, 10000000000000ULL,
100000000000000ULL, 1000000000000000ULL, 10000000000000000ULL,
100000000000000000ULL, 1000000000000000000ULL
};
uint64_t mag, t;
int neg = 0, int_digits = 0;
*ok = 0;
if (!SV_PLAIN_NUM(sv) || scale < 0 || scale > 18
|| precision < 1 || precision > 18)
return 0;
if (SvIOK(sv)) {
if (SvIsUV(sv)) {
mag = SvUVX(sv);
} else {
IV iv = SvIVX(sv);
neg = iv < 0;
mag = neg ? (uint64_t)0 - (uint64_t)iv : (uint64_t)iv;
}
for (t = mag; t; t /= 10) int_digits++;
if (int_digits > precision - scale) return 1;
mag *= p10[scale];
} else {
#ifdef HAVE_FAST_G15
NV nv = SvNVX(sv);
uint64_t q;
int e10, sh;
if (nv == 0.0) {
*raw = 0;
*ok = 1;
return 1;
}
if (!g15_digits((double)nv, &q, &e10, &neg)) return 0;
int_digits = e10 >= 0 ? e10 + 1 : 0;
if (int_digits > precision - scale) return 1;
sh = e10 - 14 + scale;
( run in 3.407 seconds using v1.01-cache-2.11-cpan-60884a8df76 )