Loading...

verticapy.sql.compute_flextable_keys#

verticapy.sql.compute_flextable_keys(flex_name: str, usecols: list | None = None) list[tuple]#

Computes the flex table keys and returns the predicted data types.

Parameters#

flex_name: str

Flex table name.

usecols: list, optional

List of columns to consider.

Returns#

List of tuples

List of virtual column names and their respective data types.

Examples#

Create a JSON file:

import json

data = {
    "column1": {
        "subcolumn1A": "value1A",
        "subcolumn1B": "value1B",
    },
    "column2": {
        "subcolumn2A": "value2A",
        "subcolumn2B": "value2B",
    }
}


json_string = json.dumps(data, indent=4)

with open("nested_columns.json", "w") as json_file:
    json_file.write(str(json_string))

We import verticapy:

import verticapy as vp

Hint

By assigning an alias to verticapy, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions from verticapy are used as intended without interfering with functions from other libraries.

We create a temporary schema:

vp.drop("temp", method = "schema")
Out[6]: True

vp.create_schema("temp")
Out[7]: True

We injest the JSON file:

vdf = vp.read_json(
    "nested_columns.json",
    schema = "temp",
    table_name = "test",
    flatten_maps = False,
    materialize = False
)

Then compute the flex table keys:

from verticapy.sql import compute_flextable_keys

compute_flextable_keys(flex_name = "temp.test")
Out[10]: [['column1', 'long varbinary(136)'], ['column2', 'long varbinary(136)']]

We drop the temporary table.

vp.drop("temp.test")
Out[11]: True

Hint

Flex tables can be used to identify all the data types needed to ingest the file and can also be employed to flatten a nested JSON file. Explore all the flex functions to understand the possibilities.

See also

compute_vmap_keys() : Computes the vmap most frequent keys.
isflextable() : Checks if the input relation is a flextable.
isvmap() : Checks if the input column is a VMap.