Skip to main content
insert_records_from_payload(table_name=None, data_text=None, data_bytes=None, modify_columns=, create_table_options=, options=)[source]

Reads from the given text-based or binary payload and inserts the data into a new or existing table. The table will be created if it doesn’t already exist.

Returns once all records are processed.

Parameters

table_name (str) –

Name of the table into which the data will be inserted, in [schema_name.]table_name format, using standard name resolution rules. If the table does not exist, the table will be created using either an existing type_id or the type inferred from the payload, and the new table name will have to meet standard table naming criteria.

data_text (str) –

Records formatted as delimited text.

data_bytes (bytes) –

Records formatted as binary data.

modify_columns (dict of str to dicts of str to str) –

Not implemented yet. The default value is an empty dict ( ).

create_table_options (dict of str to str) –

Options used when creating the target table. Includes type to use. The other options match those in GPUdb.create_table(). Allowed keys are:

  • type_id – ID of a currently registered type. The default value is ‘’.

  • no_error_if_exists – If true, prevents an error from occurring if the table already exists and is of the given type. If a table with the same ID but a different type exists, it is still an error. Allowed values are:

    • true

    • false

    The default value is ‘false’.

  • is_replicated – Affects the distribution scheme for the table’s data. If true and the given type has no explicit shard key defined, the table will be replicated. If false, the table will be sharded according to the shard key specified in the given type_id, or randomly sharded, if no shard key is specified. Note that a type containing a shard key cannot be used to create a replicated table. Allowed values are:

    • true

    • false

    The default value is ‘false’.

  • foreign_keys – Semicolon-separated list of foreign keys, of the format ‘(source_column_name [, …]) references target_table_name(primary_key_column_name [, …]) [as foreign_key_name]’.

  • foreign_shard_key – Foreign shard key of the format ‘source_column references shard_by_column from target_table(primary_key_column)’.

  • partition_typePartitioning scheme to use. Allowed values are:

  • partition_keys – Comma-separated list of partition keys, which are the columns or column expressions by which records will be assigned to partitions defined by partition_definitions.

  • partition_definitions – Comma-separated list of partition definitions, whose format depends on the choice of partition_type. See range partitioning, interval partitioning, list partitioning, hash partitioning, or series partitioning for example formats.

  • is_automatic_partition – If true, a new partition will be created for values which don’t fall into an existing partition. Currently only supported for list partitions. Allowed values are:

    • true

    • false

    The default value is ‘false’.

  • ttl – Sets the TTL of the table specified in input parameter table_name.

  • chunk_size – Indicates the number of records per chunk to be used for this table.

  • chunk_column_max_memory – Indicates the target maximum data size for each column in a chunk to be used for this table.

  • chunk_max_memory – Indicates the target maximum data size for all columns in a chunk to be used for this table.

  • is_result_table – Indicates whether the table is a memory-only table. A result table cannot contain columns with text_search data-handling, and it will not be retained if the server is restarted. Allowed values are:

    • true

    • false

    The default value is ‘false’.

  • strategy_definition – The tier strategy for the table and its columns.

  • compression_codec – The default compression codec for this table’s columns.

The default value is an empty dict ( ).

options (dict of str to str) –

Optional parameters. Allowed keys are:

  • bad_record_table_name – Name of a table to which records that were rejected are written. The bad-record-table has the following columns: line_number (long), line_rejected (string), error_message (string).

  • bad_record_table_limit – A positive integer indicating the maximum number of records that can be written to the bad-record-table. Default value is 10000.

  • bad_record_table_limit_per_input – For subscriptions: A positive integer indicating the maximum number of records that can be written to the bad-record-table per file/payload. Default value will be ‘bad_record_table_limit’ and total size of the table per rank is limited to ‘bad_record_table_limit’.

  • batch_size – Internal tuning parameter–number of records per batch when inserting data.

  • column_formats – For each target column specified, applies the column-property-bound format to the source data loaded into that column. Each column format will contain a mapping of one or more of its column properties to an appropriate format for each property. Currently supported column properties include date, time, and datetime. The parameter value must be formatted as a JSON string of maps of column names to maps of column properties to their corresponding column formats, e.g., ‘ “order_date” : “date” : “%Y.%m.%d” , “order_time” : “time” : “%H:%M:%S” ’.

    See default_column_formats for valid format syntax.

  • columns_to_load – Specifies a comma-delimited list of source-data columns that supply the target table’s columns. If more than one file is being loaded, this list applies to all files.

    Mutually exclusive with columns_to_skip.

    This list is a positional mapping onto the target table rather than a filter: the i-th entry identifies the source column that feeds the i-th column of the target table.

    Entries may be column numbers, column names, or empty.

    Column numbers are 1-based, specified discretely or as a range. For example, ‘5,7,,1..3’ inserts the fifth source column into the first target column, the seventh into the second, null into the third, and the first through third into the fourth through sixth. A range may descend (‘3..1’) to reverse that group’s order. Zero is not a valid column number. Numbers are supported only for delimited-text and Avro sources.

    Column names are strings, matching the source-data field names – either the file’s header names or the names supplied by name_columns_from_file. Requires that the source data have column names. Names are matched case-sensitively, and a name not present in the source will fail.

    An empty entry, acting as a placeholder meaning that no source column feeds the corresponding target column.

    Numbers and names cannot be mixed: a single non-numeric entry causes the entire list to be interpreted as names.

    If the target table does not exist, it is created with these columns in this order, and the list may name any subset of the source columns.

    If the target table already exists, the number of entries must equal the target table’s column count. Use empty entries to pad the list to the target’s width. Because the mapping is positional, this option can also reorder source columns into the target’s column order – for example ‘C, B, A’ for a target table whose columns are C, B, A.

    Note: specifying columns_to_load disables server-side population of target columns that no source column feeds. Such columns receive NULL instead of their default value, ‘init_with_now’, or ‘init_with_uuid’ value; if the column is non-nullable, the record is rejected. To have unfed target columns take their defaults, omit columns_to_load and rely on name-based matching, optionally with columns_to_skip.

  • columns_to_skip – Specifies a comma-delimited list of source-data columns to exclude from the load. If more than one file is being loaded, this list applies to all files.

    Mutually exclusive with columns_to_load.

    Entries may be column names matching the source-data field names (the file’s header names, or the names supplied by name_columns_from_file, matched case-sensitively), or 1-based column numbers. Numbers are supported only for delimited-text sources. Name-based entries require the source data to have column names.

    Unlike columns_to_load, this option does not change how the remaining source columns are matched to the target table. Matching remains by name and is case-insensitive, so the order and number of source columns need not correspond to the target table’s columns.

    Excluding a source column that corresponds to a target table column causes that target column to be populated from its default value, ‘init_with_*’ property, or null. This makes columns_to_skip the means of preferring a target column’s default over a value present in the source data.

    Source columns that don’t correspond to any target table column need not be listed; they are ignored.

    If the target table does not exist, the non-excluded source columns define the new table’s columns, in source-data order.

    If the source data has no column names (no header row and no name_columns_from_file) and this option is given as numbers, the remaining source columns are matched to target columns by position; the source column count must then equal the target table’s column count plus the number of columns skipped.

  • compression_type – Payload compression type. Allowed values are:

    • none – Uncompressed.

    • auto – Default. Auto detect compression type.

    • gzip – gzip file compression.

    • bzip2 – bzip2 file compression.

    The default value is ‘auto’.

  • default_column_formats – Specifies the default format to be applied to source data loaded into columns with the corresponding column property. Currently supported column properties include date, time, and datetime. This default column-property-bound format can be overridden by specifying a column property and format for a given target column in column_formats. For each specified annotation, the format will apply to all columns with that annotation unless a custom column_formats for that annotation is specified.

    The parameter value must be formatted as a JSON string that is a map of column properties to their respective column formats, e.g., ‘ “date” : “%Y.%m.%d”, “time” : “%H:%M:%S” ’. Column formats are specified as a string of control characters and plain text. The supported control characters are ‘Y’, ‘m’, ‘d’, ‘H’, ‘M’, ‘S’, and ‘s’, which follow the Linux ‘strptime()’ specification, as well as ‘s’, which specifies seconds and fractional seconds (though the fractional component will be truncated past milliseconds).

    Formats for the ‘date’ annotation must include the ‘Y’, ‘m’, and ‘d’ control characters. Formats for the ‘time’ annotation must include the ‘H’, ‘M’, and either ‘S’ or ‘s’ (but not both) control characters. Formats for the ‘datetime’ annotation meet both the ‘date’ and ‘time’ control character requirements. For example, ‘“datetime” : “%m/%d/%Y %H:%M:%S” ’ would be used to interpret text as “05/04/2000 12:12:11”

  • error_handling – Specifies how errors should be handled upon insertion. Allowed values are:

    • permissive – Records with missing columns are populated with nulls if possible; otherwise, the malformed records are skipped.

    • skip – Malformed records are skipped.

    • ignore_bad_records – Deprecated. Alias for skip.

    • abort – Stops current insertion and aborts entire operation when an error is encountered. Primary key collisions are considered abortable errors in this mode.

    The default value is ‘abort’.

  • file_type – Specifies the type of the file(s) whose records will be inserted. Allowed values are:

    • avro – Avro file format.

    • delimited_text – Delimited text file format; e.g., CSV, TSV, PSV, etc.

    • gdb – Esri/GDB file format.

    • json – JSON file format.

    • parquet – Apache Parquet file format.

    • shapefile – ShapeFile file format.

    The default value is ‘delimited_text’.

  • flatten_columns – Specifies how to handle nested columns. Allowed values are:

    • true – Break up nested columns to multiple columns.

    • false – Treat nested columns as JSON columns instead of flattening.

    The default value is ‘false’.

  • gdal_configuration_options – Comma separated list of gdal conf options, for the specific requests: key=value. The default value is ‘’.

  • pk_conflict_predicate_higher – The record with higher value for the column resolves the primary-key insert conflict. The default value is ‘’.

  • pk_conflict_predicate_lower – The record with lower value for the column resolves the primary-key insert conflict. The default value is ‘’.

  • ignore_existing_pk – Specifies the record collision error-suppression policy for inserting into a table with a primary key, only used when not in upsert mode (upsert mode is disabled when update_on_existing_pk is false). If set to true, any record being inserted that is rejected for having primary key values that match those of an existing table record will be ignored with no error generated. If false, the rejection of any record for having primary key values matching an existing record will result in an error being reported, as determined by error_handling. If the specified table does not have a primary key or if upsert mode is in effect (update_on_existing_pk is true), then this option has no effect. Allowed values are:

    • true – Ignore new records whose primary key values collide with those of existing records.

    • false – Treat as errors any new records whose primary key values collide with those of existing records.

    The default value is ‘false’.

  • ingestion_mode – Whether to do a full load, dry run, or perform a type inference on the source data. Allowed values are:

    • full – Run a type inference on the source data (if needed) and ingest.

    • dry_run – Does not load data, but walks through the source data and determines the number of valid records, taking into account the current mode of error_handling.

    • type_inference_only – Infer the type of the source data and return, without ingesting any data. The inferred type is returned in the response.

    The default value is ‘full’.

  • layer – Geo files layer(s) name(s): comma separated. The default value is ‘’.

  • loading_mode – Scheme for distributing the extraction and loading of data from the source data file(s). This option applies only when loading files that are local to the database. Allowed values are:

    • head – The head node loads all data. All files must be available to the head node.

    • distributed_shared – The head node coordinates loading data by worker processes across all nodes from shared files available to all workers.

      NOTE:

      Instead of existing on a shared source, the files can be duplicated on a source local to each host to improve performance, though the files must appear as the same data set from the perspective of all hosts performing the load.

    • distributed_local – A single worker process on each node loads all files that are available to it. This option works best when each worker loads files from its own file system, to maximize performance. In order to avoid data duplication, either each worker performing the load needs to have visibility to a set of files unique to it (no file is visible to more than one node) or the target table needs to have a primary key (which will allow the worker to automatically deduplicate data).

      NOTE:

      If the target table doesn’t exist, the table structure will be determined by the head node. If the head node has no files local to it, it will be unable to determine the structure and the request will fail.

      If the head node is configured to have no worker processes, no data strictly accessible to the head node will be loaded.

    The default value is ‘head’.

  • local_time_offset – For Avro local timestamp columns.

  • max_records_to_load – Limit the number of records to load in this request: If this number is larger than a batch_size, then the number of records loaded will be limited to the next whole number of batch_size (per working thread). The default value is ‘’.

  • name_columns_from_file – Specifies a comma-delimited list of column names to be used as the source-data column names. Supported for delimited-text sources only.

    The i-th name in this list applies to the i-th column in the file. If the file has a header row (i.e., text_has_header is true), these names override the file’s header names. If the file has no header row, these names become the source-data column names.

    Naming the source columns enables name-based matching against the target table’s columns, and permits name-based columns_to_load / columns_to_skip, which otherwise require a header row.

    Note: for a source with no header row, supplying this option changes how source columns are matched to target columns – from positional matching to matching by name. Target columns with no matching source column are then populated from their defaults or null rather than being filled positionally.

    The list is not validated against the file’s actual column count. If it is shorter, the trailing source columns are left unnamed.

  • num_tasks_per_rank – Number of tasks for reading file per rank. Default will be external_file_reader_num_tasks.

  • poll_interval – If true, the number of seconds between attempts to load external files into the table. If zero, polling will be continuous as long as data is found. If no data is found, the interval will steadily increase to a maximum of 60 seconds.

  • primary_keys – Comma separated list of column names, to set as primary keys, when not specified in the type. The default value is ‘’.

  • schema_registry_connection_retries – Confluent Schema registry connection timeout (in secs).

  • schema_registry_connection_timeout – Confluent Schema registry connection timeout (in secs).

  • schema_registry_max_consecutive_connection_failures – Max records to skip due to SR connection failures, before failing.

  • max_consecutive_invalid_schema_failure – Max records to skip due to schema related errors, before failing.

  • schema_registry_schema_name – Name of the Avro schema in the schema registry to use when reading Avro records.

  • shard_keys – Comma separated list of column names, to set as shard keys, when not specified in the type. The default value is ‘’.

  • skip_lines – Skip a number of lines from the beginning of the file.

  • subscribe – Continuously poll the data source to check for new data and load it into the table. Allowed values are:

    • true

    • false

    The default value is ‘false’.

  • table_insert_mode – When inserting records from multiple files: if table_per_file, then insert from each file into a new table. Currently supported only for shapefiles. Allowed values are:

    • single

    • table_per_file

    The default value is ‘single’.

  • text_comment_string – Specifies the character string that should be interpreted as a comment line prefix in the source data. All lines in the data starting with the provided string are ignored.

    For delimited_text file_type only. The default value is ‘#’.

  • text_delimiter – Specifies the character delimiting field values in the source data and field names in the header (if present).

    For delimited_text file_type only. The default value is ‘,’.

  • text_escape_character – Specifies the character that is used to escape other characters in the source data.

    An ‘a’, ‘b’, ‘f’, ‘n’, ‘r’, ‘t’, or ‘v’ preceded by an escape character will be interpreted as the ASCII bell, backspace, form feed, line feed, carriage return, horizontal tab, and vertical tab, respectively. For example, the escape character followed by an ‘n’ will be interpreted as a newline within a field value.

    The escape character can also be used to escape the quoting character, and will be treated as an escape character whether it is within a quoted field value or not.

    For delimited_text file_type only.

  • text_has_header – Indicates whether the source data contains a header row.

    For delimited_text file_type only. Allowed values are:

    • true

    • false

    The default value is ‘true’.

  • text_header_property_delimiter – Specifies the delimiter for column properties in the header row (if present). Cannot be set to same value as text_delimiter.

    For delimited_text file_type only. The default value is ‘|’.

  • text_null_string – Specifies the character string that should be interpreted as a null value in the source data.

    For delimited_text file_type only. The default value is ‘\N’.

  • text_quote_character – Specifies the character that should be interpreted as a field value quoting character in the source data. The character must appear at beginning and end of field value to take effect. Delimiters within quoted fields are treated as literals and not delimiters. Within a quoted field, two consecutive quote characters will be interpreted as a single literal quote character, effectively escaping it. To not have a quote character, specify an empty string.

    For delimited_text file_type only. The default value is ‘”’.

  • text_search_columns – Add ‘text_search’ property to internally inferenced string columns. Comma separated list of column names or ‘*’ for all columns. To add text_search property only to string columns of minimum size, set also the option ‘text_search_min_column_length’.

  • text_search_min_column_length – Set minimum column size. Used only when ‘text_search_columns’ has a value.

  • trim_space – If set to true, remove leading or trailing space from fields. Allowed values are:

    • true

    • false

    The default value is ‘false’.

  • truncate_strings – If set to true, truncate string values that are longer than the column’s type size. Allowed values are:

    • true

    • false

    The default value is ‘false’.

  • truncate_table – If set to true, truncates the table specified by input parameter table_name prior to loading the file(s). Allowed values are:

    • true

    • false

    The default value is ‘false’.

  • type_inference_max_records_read

  • type_inference_mode – Optimize type inference mode. Allowed values are:

    • accuracy – Scans data to get exactly-typed and sized columns for all data scanned.

    • speed – Scans data and picks the widest possible column types so that ‘all’ values will fit with minimum data scanned.

    The default value is ‘accuracy’.

  • enable_inplace_updates – Applies only when upserting (when update_on_existing_pk is true). If set to true (the default), an existing record matched by primary key is modified in place. If set to false, the matched record is updated by deleting it and inserting a replacement (delete and insert), which prevents the change from being reflected in dependent materialized views until they are refreshed. Allowed values are:

    • true

    • false

    The default value is ‘true’.

  • update_on_existing_pk – Specifies the record collision policy for inserting into a table with a primary key. If set to true, any existing table record with primary key values that match those of a record being inserted will be replaced by that new record (the new data will be “upserted”). If set to false, any existing table record with primary key values that match those of a record being inserted will remain unchanged, while the new record will be rejected and the error handled as determined by ignore_existing_pk and error_handling. If the specified table does not have a primary key, then this option has no effect. Allowed values are:

    • true – Upsert new records when primary keys match existing records.

    • false – Reject new records when primary keys match existing records.

    The default value is ‘false’.

  • transformations – Comma-separated expressions, one per target table column. Each expression is evaluated per record. Empty entries (two consecutive commas) mean no transformation for that column – the value is resolved from the input record, table default, NULL, or an error. Expressions may reference input columns by name or by position (1forthefirstinputcolumn,1 for the first input column, 2 for the second, etc.). The default value is ‘’.

The default value is an empty dict ( ).

Returns

A dict with the following entries–

table_name (str) –

Value of input parameter table_name.

type_id (str) –

ID of the currently registered table structure type for the target table.

type_definition (str) –

A JSON string describing the columns of the target table.

type_label (str) –

The user-defined description associated with the target table’s structure.

type_properties (dict of str to lists of str) –

A mapping of each target table column name to an array of column properties associated with that column.

count_inserted (long) –

Number of records inserted into the target table.

count_skipped (long) –

Number of records skipped, when not running in abort error handling mode.

count_updated (long) –

[Not yet implemented] Number of records updated within the target table.

info (dict of str to str) –

Additional information.