nxcals.api.common.utils.array_utils.StructType.add

StructType.add(field: str, data_type: str | DataType, nullable: bool = True, metadata: Dict[str, Any] | None = None) StructType
StructType.add(field: StructField) StructType

Construct a StructType by adding new elements to it, to define the schema. The method accepts either:

  1. A single parameter which is a StructField object.

  2. Between 2 and 4 parameters as (name, data_type, nullable (optional), metadata(optional). The data_type parameter may be either a String or a DataType object.

Parameters:
  • field (str or StructField) – Either the name of the field or a StructField object

  • data_type (DataType, optional) – If present, the DataType of the StructField to create

  • nullable (bool, optional) – Whether the field to add should be nullable (default True)

  • metadata (dict, optional) – Any additional metadata (default None)

Return type:

StructType

Examples

>>> from pyspark.sql.types import IntegerType, StringType, StructField, StructType
>>> struct1 = StructType().add("f1", StringType(), True).add("f2", StringType(), True, None)
>>> struct2 = StructType([StructField("f1", StringType(), True),
...     StructField("f2", StringType(), True, None)])
>>> struct1 == struct2
True
>>> struct1 = StructType().add(StructField("f1", StringType(), True))
>>> struct2 = StructType([StructField("f1", StringType(), True)])
>>> struct1 == struct2
True
>>> struct1 = StructType().add("f1", "string", True)
>>> struct2 = StructType([StructField("f1", StringType(), True)])
>>> struct1 == struct2
True