table_changes fonction table

S’applique à :case marquée oui Databricks SQL case marquée oui Databricks Runtime

Retourne un journal des modifications apportées à une table Delta Lake avec le flux de données de modifications activé.

Pour appeler cette fonction, vous devez disposer d’au moins un des éléments suivants :

  • Privilège SELECT sur la table spécifiée
  • Être le propriétaire de la table
  • Disposer de privilèges d’administration

Syntaxe

table_changes ( table_str, start [, end ] )

Arguments

  • table_str : littéral STRING représentant le nom éventuellement qualifié de la table.
  • start : littéral BIGINT ou TIMESTAMP, représentant la première version ou l’horodatage de modification à retourner.
  • end : littéral BIGINT ou TIMESTAMP facultatif, représentant la dernière version ou l’horodatage de modification à retourner. En l’absence de spécification, toutes les modifications qui se sont produites de start jusqu’à la modification actuelle sont retournées.

Retours

Tableau incluant toutes les colonnes de la table identifiées dans table_str, ainsi que les colonnes suivantes :

  • _change_type STRING NOT NULL

    Spécifie la modification : delete, insert, update_preimage ou update_postimage

  • _commit_version BIGINT NOT NULL

    Spécifie la version de validation de la table associée à la modification.

  • _commit_timestamp TIMESTAMP NOT NULL

    Spécifie l’horodatage de validation associé à la modification.

Si table_str ne représente pas de nom de table qualifié, le nom est qualifié avec la valeur current_schema. Si le nom de la table contient des espaces ou des points, utilisez des guillemets inversés dans la chaîne pour citer cette partie du nom.

Exemples

-- Create a Delta table with Change Data Feed;
> CREATE TABLE myschema.t(c1 INT, c2 STRING) TBLPROPERTIES(delta.enableChangeDataFeed=true);

-- Modify the table
> INSERT INTO myschema.t VALUES (1, 'Hello'), (2, 'World');
> INSERT INTO myschema.t VALUES (3, '!');
> UPDATE myschema.t SET c2 = upper(c2) WHERE c1 < 3;
> DELETE FROM myschema.t WHERE c1 = 3;

-- Show the history of table change events
> DESCRIBE HISTORY myschema.t;
 version timestamp                    userId           userName      operation    operationParameters                                            ...
       4 2022-09-01T18:32:35.000+0000 6167625779053302 alf@melmak.et DELETE       {"predicate":"[\"(spark_catalog.myschema.t.c1 = 3)\"]"}
       3 2022-09-01T18:32:32.000+0000 6167625779053302 alf@melmak.et UPDATE       {"predicate":"(c1#3195878 < 3)"}
       2 2022-09-01T18:32:28.000+0000 6167625779053302 alf@melmak.et WRITE        {"mode":"Append","partitionBy":"[]"}
       1 2022-09-01T18:32:26.000+0000 6167625779053302 alf@melmak.et WRITE        {"mode":"Append","partitionBy":"[]"}
       0 2022-09-01T18:32:23.000+0000 6167625779053302 alf@melmak.et CREATE TABLE {"isManaged":"true","description":null,"partitionBy":"[]","properties":"{\"delta.enableChangeDataFeed\":\"true\"}"}

-- Show the change table feed using a the commit timestamp retrieved from the history.
> SELECT * FROM table_changes('`myschema`.`t`', 2);
 c1 c2     _change_type    _commit_version _commit_timestamp
  3 !      insert                        2 2022-09-01T18:32:28.000+0000
  2 WORLD  update_postimage              3 2022-09-01T18:32:32.000+0000
  2 World  update_preimage               3 2022-09-01T18:32:32.000+0000
  1 Hello  update_preimage               3 2022-09-01T18:32:32.000+0000
  1 HELLO  update_postimage              3 2022-09-01T18:32:32.000+0000
  3 !      delete                        4 2022-09-01T18:32:35.000+0000

-- Show the ame change table feed using a point in time.
> SELECT * FROM table_changes('`myschema`.`t`', '2022-09-01T18:32:27.000+0000') ORDER BY _commit_version;
 c1 c2     _change_type    _commit_version _commit_timestamp
  3 !      insert                        2 2022-09-01T18:32:28.000+0000
  2 WORLD  update_postimage              3 2022-09-01T18:32:32.000+0000
  2 World  update_preimage               3 2022-09-01T18:32:32.000+0000
  1 Hello  update_preimage               3 2022-09-01T18:32:32.000+0000
  1 HELLO  update_postimage              3 2022-09-01T18:32:32.000+0000
  3 !      delete                        4 2022-09-01T18:32:35.000+0000