Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1riCW7-00Fgvr-KJ for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Mar 2024 12:06:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1riCW5-005AbC-0O for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Mar 2024 12:06:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1riCW4-005Ab3-NB for pgsql-hackers@lists.postgresql.org; Thu, 07 Mar 2024 12:06:29 +0000 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1riCW1-003J7j-FD for pgsql-hackers@postgresql.org; Thu, 07 Mar 2024 12:06:27 +0000 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-5d42e7ab8a9so494111a12.3 for ; Thu, 07 Mar 2024 04:06:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709813185; x=1710417985; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=9H8VExjB33hoDVO8XhEmGNYNHg2Ucu0VF36YMENYsBg=; b=AMvplNE7/+19G9i54AT+1CjgDn7mIP5shuW6QDI/PYJALVia5D6tjELCGLMUncB3GC 2ViJQZRZv9f+Xx/WEUSquYq4/fYogLpQxzyJqWv2TFvH2/2RkakxzaxoXISb9KelE/7i s/Kljvf//c/L76urWOFAsVIvsEN7wGHJW7FgS2HovbxKI+AaN9ivih+3+4zoWzzm7dXX otRxL3bfFkzf8z7yd5GEOSrgNoC1Q1OwdStfa07I28MdPxXwCtcLqiJYXWeLU8k5u7K8 Khfj/0ihgt2Py5fHZX3U2Is04MVVTMfeICjMCPGSb81YQpCMAukPwSZb3zJVLv5rLLEk ZSpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709813185; x=1710417985; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9H8VExjB33hoDVO8XhEmGNYNHg2Ucu0VF36YMENYsBg=; b=t5lhXDt234a8kLYBn9stYn+zlUjk61Lo07k8oDese6uqkgoCr9J8UFDwkXQvMi3YVx /gR80jc2l7kZjo/YfCeeevKDjGTW8OnaX//GQZLHjvfSYBzMyxkvIExip3VhawwXH+kp qtxm9Z1oFcHc4KADarVhIszQRkSAvubyIaElU0A8RteS0pZuuuUXn5BhBZhUReM7LKVH lGWawA1Ae96KNlFdCndU+CoztBMaaytcqAd5HuUGa8QyghegaR+ch1DvENAFH9BoC6sf iRA3WCItFqm322VDXFHgN06D2HkjEOeb5MTg6yb7A8WgotNi1KCXN1K7ew67FxIXBGyG sDaw== X-Forwarded-Encrypted: i=1; AJvYcCXjmyG+wwgn8ezohyWniwHOUT1LB93RmH+W0DvqmNz9WiY1tXQPfagdGc+IM6cemGNoaiQZVwS8hfCHnofwb6kq0u+VDtd8whHK87bu X-Gm-Message-State: AOJu0YyxuDLMx2SFFt1l8Ra8PUNfGlpmjyrrRphmeMtd0o9zSWHwF2M3 ffna30JUlF1KtyMhVSNoMe6vgHvXkk2ONhad+QNZlwHVtaI33u/BQTVORnIyDc3cB7iQE6sn010 s6W9HQUlVvGLMZlp36PRy1AhvmZs= X-Google-Smtp-Source: AGHT+IEVRYWG3oFMNQOCVA3BSSssbJqrVLdtqVvDv/V/4eQ02uXKRDpaGtT6W58TgJRZJPk72zt9jr2Dd9w3rNUCGwg= X-Received: by 2002:a17:90b:3115:b0:29a:ea00:9d70 with SMTP id gc21-20020a17090b311500b0029aea009d70mr14026780pjb.32.1709813184925; Thu, 07 Mar 2024 04:06:24 -0800 (PST) MIME-Version: 1.0 References: <1cee81f3-312f-30e3-0282-c4df7ee72f93@xs4all.nl> <202403041503.5momp6knupgo@alvherre.pgsql> <562e5ad9-1357-4752-ad28-7e56740f74f8@enterprisedb.com> In-Reply-To: From: Amit Langote Date: Thu, 7 Mar 2024 21:06:08 +0900 Message-ID: Subject: Re: remaining sql/json patches To: Tomas Vondra Cc: Himanshu Upadhyaya , Alvaro Herrera , Erik Rijkers , jian he , Andres Freund , Andrew Dunstan , PostgreSQL-development Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Mar 7, 2024 at 8:13=E2=80=AFPM Tomas Vondra wrote: > On 3/7/24 06:18, Himanshu Upadhyaya wrote: Thanks Himanshu for the testing. > > On Wed, Mar 6, 2024 at 9:04=E2=80=AFPM Tomas Vondra > > wrote: > >> > >> I'm pretty sure this is the correct & expected behavior. The second > >> query treats the value as string (because that's what should happen fo= r > >> values in double quotes). > >> > >> ok, Then why does the below query provide the correct conversion, eve= n if > > we enclose that in double quotes? > > =E2=80=98postgres[102531]=3D#=E2=80=99SELECT * FROM JSON_TABLE(jsonb '{ > > "id" : "1234567890", > > "FULL_NAME" : "JOHN DOE"}', > > '$' > > COLUMNS( > > name varchar(20) PATH 'lax $.FULL_NAME', > > id int PATH 'lax $.id' > > ) > > ) > > ; > > name | id > > ----------+------------ > > JOHN DOE | 1234567890 > > (1 row) > > > > and for bigger input(string) it will leave as empty as below. > > =E2=80=98postgres[102531]=3D#=E2=80=99SELECT * FROM JSON_TABLE(jsonb '{ > > "id" : "12345678901", > > "FULL_NAME" : "JOHN DOE"}', > > '$' > > COLUMNS( > > name varchar(20) PATH 'lax $.FULL_NAME', > > id int PATH 'lax $.id' > > ) > > ) > > ; > > name | id > > ----------+---- > > JOHN DOE | > > (1 row) > > > > seems it is not something to do with data enclosed in double quotes but > > somehow related with internal casting it to integer and I think in case= of > > bigger input it is not able to cast it to integer(as defined under COLU= MNS > > as id int PATH 'lax $.id') > > > > =E2=80=98postgres[102531]=3D#=E2=80=99SELECT * FROM JSON_TABLE(jsonb '{ > > "id" : "12345678901", > > "FULL_NAME" : "JOHN DOE"}', > > '$' > > COLUMNS( > > name varchar(20) PATH 'lax $.FULL_NAME', > > id int PATH 'lax $.id' > > ) > > ) > > ; > > name | id > > ----------+---- > > JOHN DOE | > > (1 row) > > ) > > > > if it is not able to represent it to integer because of bigger input, i= t > > should error out with a similar error message instead of leaving it emp= ty. > > > > Thoughts? > > > > Ah, I see! Yes, that's a bit weird. Put slightly differently: > > test=3D# SELECT * FROM JSON_TABLE(jsonb '{"id" : "2000000000"}', > '$' COLUMNS(id int PATH '$.id')); > id > ------------ > 2000000000 > (1 row) > > Time: 0.248 ms > test=3D# SELECT * FROM JSON_TABLE(jsonb '{"id" : "3000000000"}', > '$' COLUMNS(id int PATH '$.id')); > id > ---- > > (1 row) > > Clearly, when converting the string literal into int value, there's some > sort of error handling that realizes 3B overflows, and returns NULL > instead. I'm not sure if this is intentional. Indeed. This boils down to the difference in the cast expression chosen to convert the source value to int in the two cases. The case where the source value has no quotes, the chosen cast expression is a FuncExpr for function numeric_int4(), which has no way to suppress errors. When the source value has quotes, the cast expression is a CoerceViaIO expression, which can suppress the error. The default behavior is to suppress the error and return NULL, so the correct behavior is when the source value has quotes. I think we'll need either: * fix the code in 0001 to avoid getting numeric_int4() in this case, and generally cast functions that don't have soft-error handling support, in favor of using IO coercion. * fix FuncExpr (like CoerceViaIO) to respect SQL/JSON's request to suppress errors and fix downstream functions like numeric_int4() to comply by handling errors softly. I'm inclined to go with the 1st option as we already have the infrastructure in place -- input functions can all handle errors softly. For the latter, it uses numeric_int4() which doesn't support soft-error handling, so throws the error. With quotes, the -- Thanks, Amit Langote -- Thanks, Amit Langote