table_changes Funzione con valori di tabella

Si applica a:check contrassegnato con sì controllo SQL di Databricks contrassegnato con sì Databricks Runtime

Restituisce un log delle modifiche apportate a una tabella Delta Lake con feed di dati delle modifiche abilitato.

Per richiamare questa funzione è necessario avere almeno uno dei seguenti elementi:

  • SELECT privilegio sulla tabella specificata
  • Essere il proprietario della tabella
  • Disporre di privilegi amministrativi

Sintassi

table_changes ( table_str, start [, end ] )

Argomenti

  • table_str: valore letterale STRING che rappresenta il nome qualificato facoltativamente della tabella.
  • start: valore letterale BIGINT o TIMESTAMP che rappresenta la prima versione o il timestamp della modifica da restituire.
  • end: valore letterale BIGINT o TIMESTAMP facoltativo, che rappresenta l'ultima versione o il timestamp della modifica da restituire. Se non vengono specificate tutte le modifiche da start fino alla modifica corrente, vengono restituite.

Restituisce

Tabella che include tutte le colonne della tabella identificate in table_str, oltre alle colonne seguenti:

  • _change_type STRING NOT NULL

    Specifica la modifica: delete, insert, update_preimageo update_postimage

  • _commit_version BIGINT NOT NULL

    Specifica la versione commit della tabella associata alla modifica.

  • _commit_timestamp TIMESTAMP NOT NULL

    Specifica il timestamp di commit associato alla modifica.

Se table_str non rappresenta un nome di tabella qualificato, il nome è qualificato con il valore di current_schema. Se il nome della tabella contiene spazi o punti, usare le virgolette indietro all'interno della stringa per citare tale parte del nome.

Esempi

-- 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