ClickHouse-Encoder

 view release on metacpan or  search on metacpan

encode.c  view on Meta::CPAN

#endif
        if (SvIOK(*e) && !SvNOK(*e))
            leaf = SvIsUV(*e) && SvUV(*e) > (UV)INT64_MAX ? JV_FLOAT64 : JV_INT64;
        else if (SvNOK(*e)) {
            NV nv = SvNV(*e);
            leaf = (nv >= (NV)INT64_MIN && nv < (NV)9223372036854775808.0L
                    && nv == (NV)(int64_t)nv)
                 ? JV_INT64 : JV_FLOAT64;
        } else {
            leaf = JV_STRING;
        }
        if (seen_kind == -1) seen_kind = leaf;
        else if (seen_kind != leaf) return -1;
    }
    /* all-NULL or empty: default to Array(Int64). The empty array
     * doesn't carry an element type, so we pick one arbitrarily. If
     * another row in the same path has a non-empty Array(T), the kind
     * mask will then carry both Array(Int64) and Array(T); for a clean
     * single-variant column the user can declare the column explicitly
     * via a Variant(...) type. */
    if (seen_kind == -1) return JV_ARRAY_INT64;
    switch (seen_kind) {
        case JV_BOOL:    return JV_ARRAY_BOOL;
        case JV_FLOAT64: return JV_ARRAY_FLOAT64;
        case JV_INT64:   return JV_ARRAY_INT64;
        case JV_STRING:  return JV_ARRAY_STRING;
        default:         return -1;
    }
}

static int json_pkg_is_bool(const char *pkg) {
    return pkg && (strcmp(pkg, "JSON::PP::Boolean") == 0
                || strcmp(pkg, "Types::Serialiser::Boolean") == 0
                || strcmp(pkg, "JSON::XS::Boolean") == 0
                || strcmp(pkg, "Cpanel::JSON::XS::Boolean") == 0
                || strcmp(pkg, "boolean") == 0);
}

/* True if val is a blessed ref into one of the recognized boolean packages. */
static int json_is_bool_ref(pTHX_ SV *val) {
    if (!(SvROK(val) && sv_isobject(val))) return 0;
    HV *stash = SvSTASH(SvRV(val));
    return stash && json_pkg_is_bool(HvNAME(stash));
}

/* Classify a non-undef leaf SV for JSON encoding. Hash/array refs (other
 * than recognized booleans) must be rejected by the caller before this. */
static JsonValueKind json_classify_leaf(pTHX_ SV *val) {
    if (json_is_bool_ref(aTHX_ val)) return JV_BOOL;
#ifdef SvIsBOOL
    if (SvIsBOOL(val)) return JV_BOOL;
#endif
    if (SvIOK(val) && !SvNOK(val))
        return SvIsUV(val) && SvUV(val) > (UV)INT64_MAX ? JV_FLOAT64 : JV_INT64;
    if (SvNOK(val)) {
        NV n = SvNV(val);
        /* NV holding an exact integer value (e.g. `1.0`) collapses to
         * Int64 - matches CH's JSONEachRow type-inference. Users who
         * need `1.0` to survive as Float64 on the wire should put the
         * value through a float-typed Variant or an explicit Float64
         * column. NaN/Inf fall through to Float64 because the integer
         * round-trip equality check fails for them. */
        if (n >= (NV)INT64_MIN && n < (NV)9223372036854775808.0L
            && n == (NV)(int64_t)n)
            return JV_INT64;
        return JV_FLOAT64;
    }
    return JV_STRING;
}

/* Classify a (defined) JSON/Dynamic value SV. Dispatches arrayrefs to
 * json_classify_array and everything else to json_classify_leaf, so the
 * caller doesn't have to repeat the SvROK / bool-ref check. Returns the
 * JsonValueKind, or -1 if `val` is an arrayref with heterogeneous
 * elements. Caller has already ruled out hashref-non-bool refs. */
static int json_classify_value(pTHX_ SV *val) {
    if (SvROK(val) && !json_is_bool_ref(aTHX_ val))
        return json_classify_array(aTHX_ (AV*)SvRV(val));
    return json_classify_leaf(aTHX_ val);
}

/* Emit one element of an Array(T) Dynamic variant. Used by both T_JSON
 * (per-path) and T_DYNAMIC (top-level) encode paths; `ev` may be undef
 * in which case a zero-valued placeholder of the element type is written
 * (CH represents nulls via Nullable wrappers, which we don't introduce
 * inside Dynamic variant arrays). */
static void json_emit_array_elem(pTHX_ Buffer *b, SV *ev, int k_match) {
    switch (k_match) {
        case JV_ARRAY_BOOL: {
            if (!SvOK(ev)) { buf_byte(aTHX_ b, 0); break; }
            SV *bv = SvROK(ev) ? SvRV(ev) : ev;
            buf_byte(aTHX_ b, SvTRUE(bv) ? 1 : 0);
            break;
        }
        case JV_ARRAY_INT64:
            buf_le64(aTHX_ b, SvOK(ev) ? (uint64_t)(int64_t)SvIV(ev) : 0);
            break;
        case JV_ARRAY_FLOAT64:
            buf_ledouble(aTHX_ b, SvOK(ev) ? SvNV(ev) : 0.0);
            break;
        case JV_ARRAY_STRING: {
            if (!SvOK(ev)) { buf_varint(aTHX_ b, 0); break; }
            STRLEN sl;
            const char *ss = SvPV(ev, sl);
            buf_string(aTHX_ b, ss, sl);
            break;
        }
    }
}

/* Emit one scalar value of a Dynamic variant. `val` is assumed defined
 * (the disc loop has already routed undef rows to disc 0xff). */
static void json_emit_scalar(pTHX_ Buffer *b, SV *val, int k_match) {
    switch (k_match) {
        case JV_BOOL: {
            SV *bv = SvROK(val) ? SvRV(val) : val;
            buf_byte(aTHX_ b, SvTRUE(bv) ? 1 : 0);
            break;
        }
        case JV_INT64:
            buf_le64(aTHX_ b, (uint64_t)(int64_t)SvIV(val));



( run in 1.996 second using v1.01-cache-2.11-cpan-60884a8df76 )