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 1qpAhn-004e4V-UN for pgsql-novice@arkaria.postgresql.org; Sat, 07 Oct 2023 17:03:08 +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 1qpAhl-000jOq-UD for pgsql-novice@arkaria.postgresql.org; Sat, 07 Oct 2023 17:03:06 +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 1qpA1L-000VmX-Ag for pgsql-novice@lists.postgresql.org; Sat, 07 Oct 2023 16:19:16 +0000 Received: from mail-ej1-f41.google.com ([209.85.218.41]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qpA1F-000UlS-GY for pgsql-novice@lists.postgresql.org; Sat, 07 Oct 2023 16:19:15 +0000 Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-99bf3f59905so554302866b.3 for ; Sat, 07 Oct 2023 09:19:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696695548; x=1697300348; 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=Bq39auPX+z9zqysQk2lpzL82FYtYk1OiBo7Q6T/A674=; b=TwqyRAnMjz3/JahAVMgVlD1wRlBVVHsz9FhJJUrnoMyBvlB1vv9EtXDcEhpBUF5CAS y6y9RdUaq35mUA4PR/6r60weAxAcXTBkVOmFwXagbNflSsaIInNqcnKo0o64OpnxIGvQ tmkSgW4AWzOD1clv4JCeR4OuUmRgfYe6JxmtbTOwDEtY/v/ZG90hSo7WJZKdS8+NqhAk p0jvIMWv6+5loGOAZ1YAJjmxaSLo6+vGP/G45Gi8r24EKPpAfjEsyaY4/N2N0eJEuAtQ cDk+dOBBDkL01VondhrALF88kpop8qqGuWQr23baMtjupjAay/DU8x+0fDH7ewdbTUoC MsDA== X-Gm-Message-State: AOJu0YxMegAVyNuRdpfOObGFXZhcv7pI28pVLk+BGflh+pTXH2v1xX4N ty77UvEaiV5XWltbrE/iMahHq9b5Rz1I86entHM= X-Google-Smtp-Source: AGHT+IFtSM6r7ATE67zimIRNZcddyE3gyae4P9DXa1uQtnxs2eaMvjWYNaxzy1RdGnK472c6o4LVdw== X-Received: by 2002:a17:906:5341:b0:9b8:7709:6360 with SMTP id j1-20020a170906534100b009b877096360mr11306937ejo.40.1696695547493; Sat, 07 Oct 2023 09:19:07 -0700 (PDT) Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com. [209.85.221.51]) by smtp.gmail.com with ESMTPSA id o23-20020a17090611d700b009887f4e0291sm4446211eja.27.2023.10.07.09.19.06 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 07 Oct 2023 09:19:07 -0700 (PDT) Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-32167a4adaaso2776035f8f.1 for ; Sat, 07 Oct 2023 09:19:06 -0700 (PDT) X-Received: by 2002:adf:e689:0:b0:319:8bb3:ab83 with SMTP id r9-20020adfe689000000b003198bb3ab83mr9311962wrm.66.1696695546527; Sat, 07 Oct 2023 09:19:06 -0700 (PDT) MIME-Version: 1.0 References: <3126567.1696689510@sss.pgh.pa.us> In-Reply-To: <3126567.1696689510@sss.pgh.pa.us> From: jinser Date: Sun, 8 Oct 2023 00:18:55 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Is `DATE` a function? To: Tom Lane Cc: jinser , pgsql-novice@lists.postgresql.org 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 Thank you so much for your explanation. With the reminder of =E2=80=9Dcast function=E2=80=9C, I found a more detail= ed explanation in the document that I missed before: https://www.postgresql.org/docs/16/sql-expressions.html#SQL-SYNTAX-TYPE -CASTS. Thanks again everyone :) Tom Lane =E4=BA=8E2023=E5=B9=B410=E6=9C=887=E6=97=A5=E5= =91=A8=E5=85=AD 22:38=E5=86=99=E9=81=93=EF=BC=9A > > jinser writes: > > playground=3D# SELECT DATE('2022-01-13'); > > date > > ------------ > > 2022-01-13 > > (1 row) > > Sure, there are functions named date(): > > postgres=3D# \df date > List of functions > Schema | Name | Result data type | Argument data types | Typ= e > ------------+------+------------------+-----------------------------+----= -- > pg_catalog | date | date | timestamp with time zone | fun= c > pg_catalog | date | date | timestamp without time zone | fun= c > (2 rows) > > The reason these aren't explicitly documented is that they are intended a= s > implementation support for casts. > > postgres=3D# \dC date > List of casts > Source type | Target type | Function = | Implicit? > -----------------------------+-----------------------------+-------------= +--------------- > date | timestamp with time zone | timestamptz = | yes > date | timestamp without time zone | timestamp = | yes > timestamp with time zone | date | date = | in assignment > timestamp without time zone | date | date = | in assignment > (4 rows) > > Hence, the preferred spelling is more like > > select now()::date; > > or if you want to be SQL-spec-compatible, > > select cast(now() as date); > > but for historical reasons we like to let you also write > > select date(now()); > > which is managed (in most cases) by naming cast implementation > functions the same as the target type. > > > Another reason I think this is a function is that other types don't > > seem to have the same behavior: > > > playground=3D# SELECT integer('123'); > > ERROR: syntax error at or near "(" > > You're running into a couple of things there: INTEGER is a reserved > word, and the cast functions for that type are named after the > internal type name "int4". > > postgres=3D# \dC integer > List of casts > Source type | Target type | Function | Implicit? > ------------------+------------------+--------------------+--------------= - > "char" | integer | int4 | no > bigint | integer | int4 | in assignment > bit | integer | int4 | no > boolean | integer | int4 | no > double precision | integer | int4 | in assignment > integer | "char" | char | no > ... > > postgres=3D# select int4('123'); > int4 > ------ > 123 > (1 row) > > Note that none of these have anything to do with the syntax for > a typed literal, which is "type-name quoted-literal" with no > parentheses: > > postgres=3D# select date 'today'; > date > ------------ > 2023-10-07 > (1 row) > > postgres=3D# select integer '42'; > int4 > ------ > 42 > (1 row) > > Some aspects of the behavior might look the same, but there > are a lot of edge cases. > > regards, tom lane >