Loading...

vastorbit.get_data_types

vastorbit.get_data_types(expr: str | None = None, column: str | None = None, table_name: str | None = None, schema: str | None = None, catalog: str | None = None, usecols: List[str] | None = None) str | List[Tuple[str, str]]

Returns customized relation columns and the respective data types. This process may create a temporary table or use DESCRIBE for metadata.

If table_name is defined, the expression is ignored and the function returns the table/ view column names and data types.

Parameters:
  • expr (str, optional) – An expression in pure SQL. If empty, the parameter ‘table_name’ must be defined.

  • column (str, optional) – If not empty, the function returns only the data type of the input column if it is in the relation.

  • table_name (str, optional) – Input table Name.

  • schema (str, optional) – Table schema.

  • catalog (str, optional) – Table catalog (Trino-specific).

  • usecols (list, optional) – List of columns to consider. This parameter cannot be used if ‘column’ is defined.

Returns:

If column is specified, returns the data type string. Otherwise, returns a list of (column_name, data_type) tuples.

Return type:

str or list of tuples

Examples

Let’s import the function before proceeding.

from vastorbit.sql import get_data_types

You can easily retrieve all the types of the columns in your SQL query by using this function.

get_data_types(
    "SELECT "
    "pclass, embarked, AVG(survived) "
    "FROM titanic "
    "GROUP BY 1, 2"
)

You can also retrieve the type of a specific column.

get_data_types(
    "SELECT "
    "pclass, embarked, AVG(survived) "
    "FROM titanic "
    "GROUP BY 1, 2",
    column = "pclass",
)

Note

This function is employed to determine the column types in a SQL query. For tables and views, it uses Trino’s DESCRIBE or information_schema. For SQL queries, it uses the cursor description after executing a LIMIT 0 query.

See also

trino_dtype() : Formats the input data type.