Skip to main content
Ctrl+K

документация Tantor Special Edition 15.12

Оглавление:

  • Preface
    • 1.  What Is Tantor SE?
    • 2. A Brief History of PostgreSQL
    • 3. What are the differences between Tantor SE 15 and PostgreSQL 15
    • 4. Conventions
    • 5. Bug Reporting Guidelines
  • Часть I. Tutorial
    • Глава 1. Synopsis
    • Глава 2. The SQL Language
    • Глава 3. Advanced Features
  • Часть II. The SQL Language
    • Глава 4. SQL Syntax
    • Глава 5. Data Definition
    • Глава 6. Data Manipulation
    • Глава 7. Queries
    • Глава 8. Data Types
    • Глава 9. Functions and Operators
    • Глава 10. Type Conversion
    • Глава 11. Indexes
    • Глава 12. Full Text Search
    • Глава 13. Concurrency Control
    • Глава 14. Performance Tips
    • Глава 15. Parallel Query
    • Глава 16. Autonomous Transactions
  • Часть III. Server Administration
    • Глава 17. Installation from Binaries
    • Глава 18. Server Setup and Operation
    • Глава 19. Server Configuration
    • Глава 20. Client Authentication
    • Глава 21. Database Roles
    • Глава 22. Managing Databases
    • Глава 23. Localization
    • Глава 24. Routine Database Maintenance Tasks
    • Глава 25. Backup and Restore
    • Глава 26. High Availability, Load Balancing, and Replication
    • Глава 27. Monitoring Database Activity
    • Глава 28. Monitoring Disk Usage
    • Глава 29. Reliability and the Write-Ahead Log
    • Глава 30. Logical Replication
    • Глава 31. Just-in-Time Compilation (JIT)
  • Часть IV. Client Interfaces
    • Глава 32. libpq — C Library
    • Глава 33. Large Objects
    • Глава 34. ECPG — Embedded SQL in C
    • Глава 35. The Information Schema
  • Часть V. Server Programming
    • Глава 36. Extending SQL
    • Глава 37. Triggers
    • Глава 38. Event Triggers
    • Глава 39. The Rule System
    • Глава 40. Procedural Languages
    • Глава 41. PL/pgSQL — SQL Procedural Language
    • Глава 42. PL/Tcl — Tcl Procedural Language
    • Глава 43. PL/Perl — Perl Procedural Language
    • Глава 44. PL/Python — Python Procedural Language
    • Глава 45. Server Programming Interface
    • Глава 46. Background Worker Processes
    • Глава 47. Logical Decoding
    • Глава 48. Replication Progress Tracking
    • Глава 49. Archive Modules
  • Часть VI. Reference
    • SQL Commands
    • PostgreSQL Client Applications
    • PostgreSQL Server Applications
  • Часть VII. Internals
    • Глава 50. Overview of PostgreSQL Internals
    • Глава 51. System Catalogs
    • Глава 52. System Views
    • Глава 53. Frontend/Backend Protocol
    • Глава 54. PostgreSQL Coding Conventions
    • Глава 55. Native Language Support
    • Глава 56. Writing a Procedural Language Handler
    • Глава 57. Writing a Foreign Data Wrapper
    • Глава 58. Writing a Table Sampling Method
    • Глава 59. Writing a Custom Scan Provider
    • Глава 60. Genetic Query Optimizer
    • Глава 61. Table Access Method Interface Definition
    • Глава 62. Index Access Method Interface Definition
    • Глава 63. Generic WAL Records
    • Глава 64. Custom WAL Resource Managers
    • Глава 65. B-Tree Indexes
    • Глава 66. GiST Indexes
    • Глава 67. SP-GiST Indexes
    • Глава 68. GIN Indexes
    • Глава 69. BRIN Indexes
    • Глава 70. Hash Indexes
    • Глава 71. Database Physical Storage
    • Глава 72. System Catalog Declarations and Initial Contents
    • Глава 73. How the Planner Uses Statistics
    • Глава 74. Backup Manifest Format
  • Часть VIII. Appendixes
    • Предметный указатель A. Tantor SE Error Codes
    • Предметный указатель B. Date/Time Support
    • Предметный указатель C. SQL Key Words
    • Предметный указатель D. SQL Conformance
    • Предметный указатель E. Release Notes
    • Предметный указатель F. Additional Supplied Modules
    • Предметный указатель G. Additional Supplied Programs
    • Предметный указатель H. Additional External Modules
    • Предметный указатель I. External Projects
    • Предметный указатель J. Tantor SE Limits
    • Предметный указатель K. Acronyms
    • Предметный указатель L. Glossary
    • Предметный указатель M. Color Support
    • Предметный указатель N. Obsolete or Renamed Features
  • Bibliography
  • Предметный указатель

10.5. UNION, CASE, and Related Constructs

10.5. UNION, CASE, and Related Constructs#

10.5. UNION, CASE, and Related Constructs
10.5. UNION, CASE, and Related Constructs
Назад НаверхГлава 10. Type ConversionНачало Далее

10.5. UNION, CASE, and Related Constructs

SQL UNION constructs must match up possibly dissimilar types to become a single result set. The resolution algorithm is applied separately to each output column of a union query. The INTERSECT and EXCEPT constructs resolve dissimilar types in the same way as UNION. Some other constructs, including CASE, ARRAY, VALUES, and the GREATEST and LEAST functions, use the identical algorithm to match up their component expressions and select a result data type.

Type Resolution for UNION, CASE, and Related Constructs

  1. If all inputs are of the same type, and it is not unknown, resolve as that type.

  2. If any input is of a domain type, treat it as being of the domain's base type for all subsequent steps. [12]

  3. If all inputs are of type unknown, resolve as type text (the preferred type of the string category). Otherwise, unknown inputs are ignored for the purposes of the remaining rules.

  4. If the non-unknown inputs are not all of the same type category, fail.

  5. Select the first non-unknown input type as the candidate type, then consider each other non-unknown input type, left to right. [13] If the candidate type can be implicitly converted to the other type, but not vice-versa, select the other type as the new candidate type. Then continue considering the remaining inputs. If, at any stage of this process, a preferred type is selected, stop considering additional inputs.

  6. Convert all inputs to the final candidate type. Fail if there is not an implicit conversion from a given input type to the candidate type.

Some examples follow.

Пример 10.10. Type Resolution with Underspecified Types in a Union

SELECT text 'a' AS "text" UNION SELECT 'b';

 text
------
 a
 b
(2 rows)

Here, the unknown-type literal 'b' will be resolved to type text.


Пример 10.11. Type Resolution in a Simple Union

SELECT 1.2 AS "numeric" UNION SELECT 1;

 numeric
---------
       1
     1.2
(2 rows)

The literal 1.2 is of type numeric, and the integer value 1 can be cast implicitly to numeric, so that type is used.


Пример 10.12. Type Resolution in a Transposed Union

SELECT 1 AS "real" UNION SELECT CAST('2.2' AS REAL);

 real
------
    1
  2.2
(2 rows)

Here, since type real cannot be implicitly cast to integer, but integer can be implicitly cast to real, the union result type is resolved as real.


Пример 10.13. Type Resolution in a Nested Union

SELECT NULL UNION SELECT NULL UNION SELECT 1;

ERROR:  UNION types text and integer cannot be matched

This failure occurs because Tantor SE treats multiple UNIONs as a nest of pairwise operations; that is, this input is the same as

(SELECT NULL UNION SELECT NULL) UNION SELECT 1;

The inner UNION is resolved as emitting type text, according to the rules given above. Then the outer UNION has inputs of types text and integer, leading to the observed error. The problem can be fixed by ensuring that the leftmost UNION has at least one input of the desired result type.

INTERSECT and EXCEPT operations are likewise resolved pairwise. However, the other constructs described in this section consider all of their inputs in one resolution step.




[12] Somewhat like the treatment of domain inputs for operators and functions, this behavior allows a domain type to be preserved through a UNION or similar construct, so long as the user is careful to ensure that all inputs are implicitly or explicitly of that exact type. Otherwise the domain's base type will be used.

[13] For historical reasons, CASE treats its ELSE clause (if any) as the “first” input, with the THEN clauses(s) considered after that. In all other cases, “left to right” means the order in which the expressions appear in the query text.


Назад Наверх Далее
10.4. Value Storage Начало 10.6. SELECT Output Columns
Коммерческое использование возможно только с письменного разрешения компании ОOO “Лаборатории Тантор”
© ООО "Лаборатории Тантор"
  • Положение о технической поддержке
  • Политика обработки файлов сookie
  • Пользовательское соглашение сайта
Продукты
  • СУБД Tantor
  • Платформа Tantor
  • Документация
Контакты
  • +7 495 369-48-16
  • info@tantorlabs.ru
15.12.0 - 77d56436 - 2025-07-18 14:56:16