Greenplum obtains the field name of an SQL result.

Source: Internet
Author: User
In Greenplum, it is difficult to obtain the field names after execution of any SQL statement. For example, when writing a common tool, use the copy command

In Greenplum, it is difficult to obtain the field names after execution of any SQL statement. For example, when writing a common tool, use the copy command

In Greenplum, it is difficult to obtain the field names after execution of any SQL statement.

For example, when writing a common tool, you can use the copy command to export an SQL statement to text, but the text exported by the name of each field does not exist, if you use your own SQL parsing, it will be too complicated.

If we want to obtain these field names, we do not actually execute the SQL statement, because the execution plan has been generated, and I know the following methods:

1. Use JDBC and prepareStatement to generate an execution plan and obtain the field name:

Ps: it is not just JDBC, but other client interfaces are estimated to have such interfaces to retrieve fields. After reading the jdbc source code, when obtaining the metaData information, it directly interacts with the database and receives the information. For others, there should be a defined interface, which needs to be mined by yourself.


2. If verbose is added to the explain statement, the number of parsed syntaxes is printed. As follows:

The top targetlist is the list of fields to be returned by SQL.

In this way, can we use this to write a function to obtain the field name? It is still difficult to parse the syntax tree directly. We found that each field name must start with resname. We can traverse this number and find all the fields starting with ": resname", that is, the field name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.