Home Explore Blog CI



postgresql

27th chunk of `doc/src/sgml/syntax.sgml`
7af9dba4f1b68a4db4ebbad9b42a6aec33aeb5caf9cce8ca0000000100000fa4
 <literal>FILTER</literal> clause.
   </para>

   <para>
    The built-in window functions are described in <xref
    linkend="functions-window-table"/>.  Other window functions can be added by
    the user.  Also, any built-in or user-defined general-purpose or
    statistical aggregate can be used as a window function.  (Ordered-set
    and hypothetical-set aggregates cannot presently be used as window functions.)
   </para>

   <para>
    The syntaxes using <literal>*</literal> are used for calling parameter-less
    aggregate functions as window functions, for example
    <literal>count(*) OVER (PARTITION BY x ORDER BY y)</literal>.
    The asterisk (<literal>*</literal>) is customarily not used for
    window-specific functions.  Window-specific functions do not
    allow <literal>DISTINCT</literal> or <literal>ORDER BY</literal> to be used within the
    function argument list.
   </para>

   <para>
    Window function calls are permitted only in the <literal>SELECT</literal>
    list and the <literal>ORDER BY</literal> clause of the query.
   </para>

   <para>
    More information about window functions can be found in
    <xref linkend="tutorial-window"/>,
    <xref linkend="functions-window"/>, and
    <xref linkend="queries-window"/>.
   </para>
  </sect2>

  <sect2 id="sql-syntax-type-casts">
   <title>Type Casts</title>

   <indexterm>
    <primary>data type</primary>
    <secondary>type cast</secondary>
   </indexterm>

   <indexterm>
    <primary>type cast</primary>
   </indexterm>

   <indexterm>
    <primary>::</primary>
   </indexterm>

   <para>
    A type cast specifies a conversion from one data type to another.
    <productname>PostgreSQL</productname> accepts two equivalent syntaxes
    for type casts:
<synopsis>
CAST ( <replaceable>expression</replaceable> AS <replaceable>type</replaceable> )
<replaceable>expression</replaceable>::<replaceable>type</replaceable>
</synopsis>
    The <literal>CAST</literal> syntax conforms to SQL; the syntax with
    <literal>::</literal> is historical <productname>PostgreSQL</productname>
    usage.
   </para>

   <para>
    When a cast is applied to a value expression of a known type, it
    represents a run-time type conversion.  The cast will succeed only
    if a suitable type conversion operation has been defined.  Notice that this
    is subtly different from the use of casts with constants, as shown in
    <xref linkend="sql-syntax-constants-generic"/>.  A cast applied to an
    unadorned string literal represents the initial assignment of a type
    to a literal constant value, and so it will succeed for any type
    (if the contents of the string literal are acceptable input syntax for the
    data type).
   </para>

   <para>
    An explicit type cast can usually be omitted if there is no ambiguity as
    to the type that a value expression must produce (for example, when it is
    assigned to a table column); the system will automatically apply a
    type cast in such cases.  However, automatic casting is only done for
    casts that are marked <quote>OK to apply implicitly</quote>
    in the system catalogs.  Other casts must be invoked with
    explicit casting syntax.  This restriction is intended to prevent
    surprising conversions from being applied silently.
   </para>

   <para>
    It is also possible to specify a type cast using a function-like
    syntax:
<synopsis>
<replaceable>typename</replaceable> ( <replaceable>expression</replaceable> )
</synopsis>
    However, this only works for types whose names are also valid as
    function names.  For example, <literal>double precision</literal>
    cannot be used this way, but the equivalent <literal>float8</literal>
    can.  Also, the names <literal>interval</literal>, <literal>time</literal>, and
    <literal>timestamp</literal> can only be used in this fashion if they are
    double-quoted, because of syntactic conflicts.  Therefore, the use of
    the function-like cast syntax leads to inconsistencies

Title: Window Functions and Type Casts in PostgreSQL
Summary
This section concludes the discussion on window functions, pointing to additional resources. It then introduces type casts in PostgreSQL, covering syntax (CAST(... AS ...) and expression::type), run-time conversion, and the difference between casting value expressions and constants. The text specifies when explicit type casts can be omitted and how automatic casting is restricted to implicitly applicable casts. The section also explains function-like type cast syntax (typename(expression)), noting its limitations, especially with type names that conflict with function names.