|
| 1 | +package com.linkedin.venice.sql; |
| 2 | + |
| 3 | +import static com.linkedin.venice.sql.AvroToSQL.UnsupportedTypeHandling.FAIL; |
| 4 | + |
| 5 | +import com.linkedin.davinci.client.DaVinciRecordTransformer; |
| 6 | +import com.linkedin.davinci.client.DaVinciRecordTransformerResult; |
| 7 | +import com.linkedin.venice.utils.lazy.Lazy; |
| 8 | +import java.sql.Connection; |
| 9 | +import java.sql.DriverManager; |
| 10 | +import java.sql.PreparedStatement; |
| 11 | +import java.sql.SQLException; |
| 12 | +import java.sql.Statement; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.function.BiConsumer; |
| 16 | +import org.apache.avro.Schema; |
| 17 | +import org.apache.avro.SchemaBuilder; |
| 18 | +import org.apache.avro.generic.GenericRecord; |
| 19 | + |
| 20 | + |
| 21 | +public class DuckDBDaVinciRecordTransformer extends DaVinciRecordTransformer<String, GenericRecord, GenericRecord> { |
| 22 | + private static final String duckDBFilePath = "my_database.duckdb"; |
| 23 | + // ToDo: Don't hardcode the table name. Get it from the storeName |
| 24 | + private static final String baseVersionTableName = "my_table_v"; |
| 25 | + private static final Set<String> primaryKeys = Collections.singleton("firstName"); |
| 26 | + private static final String deleteStatementTemplate = "DELETE FROM %s WHERE %s = ?;"; |
| 27 | + private static final String createViewStatementTemplate = |
| 28 | + "CREATE OR REPLACE VIEW current_version AS SELECT * FROM %s;"; |
| 29 | + private static final String dropTableStatementTemplate = "DROP TABLE %s;"; |
| 30 | + private final String duckDBUrl; |
| 31 | + private final String versionTableName; |
| 32 | + |
| 33 | + public DuckDBDaVinciRecordTransformer(int storeVersion, boolean storeRecordsInDaVinci, String baseDir) { |
| 34 | + super(storeVersion, storeRecordsInDaVinci); |
| 35 | + versionTableName = baseVersionTableName + storeVersion; |
| 36 | + duckDBUrl = "jdbc:duckdb:" + baseDir + "/" + duckDBFilePath; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Schema getKeySchema() { |
| 41 | + return Schema.create(Schema.Type.STRING); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Schema getOutputValueSchema() { |
| 46 | + return SchemaBuilder.record("nameRecord") |
| 47 | + .namespace("example.avro") |
| 48 | + .fields() |
| 49 | + .name("firstName") |
| 50 | + .type() |
| 51 | + .stringType() |
| 52 | + .stringDefault("") |
| 53 | + .name("lastName") |
| 54 | + .type() |
| 55 | + .stringType() |
| 56 | + .stringDefault("") |
| 57 | + .endRecord(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public DaVinciRecordTransformerResult<GenericRecord> transform(Lazy<String> key, Lazy<GenericRecord> value) { |
| 62 | + // Record transformation happens inside processPut as we need access to the connection object to create the prepared |
| 63 | + // statement |
| 64 | + return new DaVinciRecordTransformerResult<>(DaVinciRecordTransformerResult.Result.UNCHANGED); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void processPut(Lazy<String> key, Lazy<GenericRecord> value) { |
| 69 | + Schema valueSchema = value.get().getSchema(); |
| 70 | + String upsertStatement = AvroToSQL.upsertStatement(versionTableName, valueSchema); |
| 71 | + |
| 72 | + // ToDo: Instead of creating a connection on every call, have a long-term connection. Maybe a connection pool? |
| 73 | + try (Connection connection = DriverManager.getConnection(duckDBUrl)) { |
| 74 | + BiConsumer<GenericRecord, PreparedStatement> upsertProcessor = AvroToSQL.upsertProcessor(valueSchema); |
| 75 | + |
| 76 | + try (PreparedStatement preparedStatement = connection.prepareStatement(upsertStatement)) { |
| 77 | + upsertProcessor.accept(value.get(), preparedStatement); |
| 78 | + } |
| 79 | + } catch (SQLException e) { |
| 80 | + throw new RuntimeException(e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void processDelete(Lazy<String> key) { |
| 86 | + // Unable to convert to prepared statement as table and column names can't be parameterized |
| 87 | + // ToDo make delete non-hardcoded on primaryKey |
| 88 | + String deleteStatement = String.format(deleteStatementTemplate, versionTableName, "firstName"); |
| 89 | + |
| 90 | + // ToDo: Instead of creating a connection on every call, have a long-term connection. Maybe a connection pool? |
| 91 | + try (Connection connection = DriverManager.getConnection(duckDBUrl); |
| 92 | + PreparedStatement stmt = connection.prepareStatement(deleteStatement)) { |
| 93 | + stmt.setString(1, key.get()); |
| 94 | + stmt.execute(); |
| 95 | + } catch (SQLException e) { |
| 96 | + throw new RuntimeException(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void onStartVersionIngestion(boolean isCurrentVersion) { |
| 102 | + try (Connection connection = DriverManager.getConnection(duckDBUrl); |
| 103 | + Statement stmt = connection.createStatement()) { |
| 104 | + String createTableStatement = |
| 105 | + AvroToSQL.createTableStatement(versionTableName, getOutputValueSchema(), primaryKeys, FAIL); |
| 106 | + stmt.execute(createTableStatement); |
| 107 | + |
| 108 | + if (isCurrentVersion) { |
| 109 | + // Unable to convert to prepared statement as table and column names can't be parameterized |
| 110 | + String createViewStatement = String.format(createViewStatementTemplate, versionTableName); |
| 111 | + stmt.execute(createViewStatement); |
| 112 | + } |
| 113 | + } catch (SQLException e) { |
| 114 | + throw new RuntimeException(e); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onEndVersionIngestion(int currentVersion) { |
| 120 | + try (Connection connection = DriverManager.getConnection(duckDBUrl); |
| 121 | + Statement stmt = connection.createStatement()) { |
| 122 | + // Swap to current version |
| 123 | + String currentVersionTableName = baseVersionTableName + currentVersion; |
| 124 | + String createViewStatement = String.format(createViewStatementTemplate, currentVersionTableName); |
| 125 | + stmt.execute(createViewStatement); |
| 126 | + |
| 127 | + // Unable to convert to prepared statement as table and column names can't be parameterized |
| 128 | + // Drop DuckDB table for storeVersion as it's retired |
| 129 | + String dropTableStatement = String.format(dropTableStatementTemplate, versionTableName); |
| 130 | + stmt.execute(dropTableStatement); |
| 131 | + } catch (SQLException e) { |
| 132 | + throw new RuntimeException(e); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public String getDuckDBUrl() { |
| 137 | + return duckDBUrl; |
| 138 | + } |
| 139 | +} |
0 commit comments