|
7 | 7 | import java.io.FileOutputStream; |
8 | 8 | import java.io.IOException; |
9 | 9 | import java.io.InputStream; |
| 10 | +import java.math.BigDecimal; |
10 | 11 | import java.net.MalformedURLException; |
11 | 12 | import java.net.URI; |
12 | 13 | import java.net.URISyntaxException; |
|
15 | 16 | import java.nio.file.Files; |
16 | 17 | import java.nio.file.InvalidPathException; |
17 | 18 | import java.nio.file.Path; |
18 | | -import java.math.BigDecimal; |
19 | 19 | import java.time.LocalDate; |
20 | 20 | import java.time.LocalTime; |
21 | 21 | import java.time.ZonedDateTime; |
|
25 | 25 | import java.util.logging.Level; |
26 | 26 | import java.util.logging.Logger; |
27 | 27 | import java.util.stream.IntStream; |
28 | | - |
29 | 28 | import org.enso.table.data.column.storage.ColumnBooleanStorage; |
30 | 29 | import org.enso.table.data.column.storage.ColumnDoubleStorage; |
31 | 30 | import org.enso.table.data.column.storage.ColumnLongStorage; |
32 | 31 | import org.enso.table.data.column.storage.type.BigDecimalType; |
33 | | -import org.enso.table.data.column.storage.type.IntegerType; |
34 | | -import org.enso.table.data.column.storage.type.TextType; |
35 | | -import org.enso.table.data.column.storage.type.FloatType; |
36 | 32 | import org.enso.table.data.column.storage.type.BooleanType; |
37 | 33 | import org.enso.table.data.column.storage.type.DateTimeType; |
38 | 34 | import org.enso.table.data.column.storage.type.DateType; |
| 35 | +import org.enso.table.data.column.storage.type.FloatType; |
| 36 | +import org.enso.table.data.column.storage.type.IntegerType; |
| 37 | +import org.enso.table.data.column.storage.type.TextType; |
39 | 38 | import org.enso.table.data.column.storage.type.TimeOfDayType; |
40 | 39 | import org.enso.table.data.table.Column; |
41 | 40 | import org.enso.table.data.table.Table; |
@@ -331,74 +330,79 @@ public static Column[] readTable( |
331 | 330 | } |
332 | 331 | } |
333 | 332 |
|
334 | | - public static void writeTable(String path, String schemaName, String tableName, Table table) throws IOException { |
| 333 | + public static void writeTable(String path, String schemaName, String tableName, Table table) |
| 334 | + throws IOException { |
335 | 335 | getProcess(); |
336 | | - try (var connection = new Connection(process.getEndpoint(), path, CreateMode.CREATE_IF_NOT_EXISTS)) { |
| 336 | + try (var connection = |
| 337 | + new Connection(process.getEndpoint(), path, CreateMode.CREATE_IF_NOT_EXISTS)) { |
337 | 338 | var tableDef = createTable(schemaName, tableName, table.getColumns(), connection); |
338 | 339 | insertData(table, tableDef, connection); |
339 | 340 | } |
340 | 341 | } |
341 | 342 |
|
342 | | - private static TableDefinition createTable(String schemaName, String tableName, Column[] columns, Connection connection) { |
343 | | - final var sn = new SchemaName(schemaName); |
344 | | - if (!connection.getCatalog().getSchemaNames().contains(sn)) { |
345 | | - connection.getCatalog().createSchema(sn); |
346 | | - } |
347 | | - |
348 | | - var tableDef = new TableDefinition(new TableName(schemaName, tableName)); |
349 | | - for (var col : columns) { |
350 | | - String columnName = col.getName(); |
351 | | - var storage = col.getStorage(); |
352 | | - switch (storage.getType()) { |
353 | | - case TextType _ -> tableDef.addColumn(columnName, SqlType.text()); |
354 | | - case IntegerType _ -> tableDef.addColumn(columnName, SqlType.bigInt()); |
355 | | - case FloatType _ -> tableDef.addColumn(columnName, SqlType.doublePrecision()); |
356 | | - case BooleanType _ -> tableDef.addColumn(columnName, SqlType.bool()); |
357 | | - case DateType _ -> tableDef.addColumn(columnName, SqlType.date()); |
358 | | - case TimeOfDayType _ -> tableDef.addColumn(columnName, SqlType.time()); |
359 | | - case DateTimeType _ -> tableDef.addColumn(columnName, SqlType.timestampTz()); |
| 343 | + private static TableDefinition createTable( |
| 344 | + String schemaName, String tableName, Column[] columns, Connection connection) { |
| 345 | + final var sn = new SchemaName(schemaName); |
| 346 | + if (!connection.getCatalog().getSchemaNames().contains(sn)) { |
| 347 | + connection.getCatalog().createSchema(sn); |
| 348 | + } |
| 349 | + |
| 350 | + var tableDef = new TableDefinition(new TableName(schemaName, tableName)); |
| 351 | + for (var col : columns) { |
| 352 | + String columnName = col.getName(); |
| 353 | + var storage = col.getStorage(); |
| 354 | + switch (storage.getType()) { |
| 355 | + case TextType t -> tableDef.addColumn(columnName, SqlType.text()); |
| 356 | + case IntegerType t -> tableDef.addColumn(columnName, SqlType.bigInt()); |
| 357 | + case FloatType t -> tableDef.addColumn(columnName, SqlType.doublePrecision()); |
| 358 | + case BooleanType t -> tableDef.addColumn(columnName, SqlType.bool()); |
| 359 | + case DateType t -> tableDef.addColumn(columnName, SqlType.date()); |
| 360 | + case TimeOfDayType t -> tableDef.addColumn(columnName, SqlType.time()); |
| 361 | + case DateTimeType t -> tableDef.addColumn(columnName, SqlType.timestampTz()); |
360 | 362 | // https://tableau.github.io/hyper-db/docs/sql/datatype/numeric |
361 | | - // Precisions over 18 require 128-bit for internal storage. Processing 128-bit numeric values is |
362 | | - // often slower than processing 64-bit values, so it is advisable to use a sensible precision for |
| 363 | + // Precisions over 18 require 128-bit for internal storage. Processing 128-bit numeric |
| 364 | + // values is |
| 365 | + // often slower than processing 64-bit values, so it is advisable to use a sensible |
| 366 | + // precision for |
363 | 367 | // the use case at hand instead of always using the maximum precision by default. |
364 | | - case BigDecimalType _ -> tableDef.addColumn(columnName, SqlType.numeric(18, 9)); |
365 | | - default -> throw new HyperUnsupportedTypeError(storage.getType().toString()); |
366 | | - } |
| 368 | + case BigDecimalType t -> tableDef.addColumn(columnName, SqlType.numeric(18, 9)); |
| 369 | + default -> throw new HyperUnsupportedTypeError(storage.getType().toString()); |
367 | 370 | } |
368 | | - connection.executeCommand("DROP TABLE IF EXISTS \""+schemaName+"\".\""+tableName+"\""); |
369 | | - connection.getCatalog().createTable(tableDef); |
370 | | - return tableDef; |
| 371 | + } |
| 372 | + connection.executeCommand("DROP TABLE IF EXISTS \"" + schemaName + "\".\"" + tableName + "\""); |
| 373 | + connection.getCatalog().createTable(tableDef); |
| 374 | + return tableDef; |
371 | 375 | } |
372 | 376 |
|
373 | | - private static void insertData(Table table, TableDefinition tableDef, Connection connection){ |
374 | | - int numberOfRows = table.rowCount(); |
375 | | - int numberOfColumns = table.getColumns().length; |
376 | | - Inserter inserter = new Inserter(connection, tableDef); |
377 | | - for (int row = 0; row < numberOfRows; ++row) { |
378 | | - for (int col = 0; col < numberOfColumns; ++col) { |
379 | | - var storage = table.getColumns()[col].getStorage(); |
380 | | - if (storage.isNothing(row)) { |
381 | | - inserter.addNull(); |
382 | | - } else if (storage instanceof ColumnDoubleStorage doubleStorage) { |
383 | | - inserter.add(doubleStorage.getItemAsDouble(row)); |
384 | | - } else if (storage instanceof ColumnLongStorage longStorage) { |
385 | | - inserter.add(longStorage.getItemAsLong(row)); |
386 | | - } else if (storage instanceof ColumnBooleanStorage boolStorage) { |
387 | | - inserter.add(boolStorage.getItemAsBoolean(row)); |
388 | | - } else { |
389 | | - Object value = storage.getItemBoxed(row); |
390 | | - switch (value) { |
391 | | - case String s -> inserter.add(s); |
392 | | - case LocalDate ld -> inserter.add(ld); |
393 | | - case LocalTime lt -> inserter.add(lt); |
394 | | - case ZonedDateTime zdt -> inserter.add(zdt); |
395 | | - case BigDecimal bd -> inserter.add(bd); |
396 | | - default -> throw new HyperUnsupportedTypeError(value.toString()); |
397 | | - } |
| 377 | + private static void insertData(Table table, TableDefinition tableDef, Connection connection) { |
| 378 | + int numberOfRows = table.rowCount(); |
| 379 | + int numberOfColumns = table.getColumns().length; |
| 380 | + Inserter inserter = new Inserter(connection, tableDef); |
| 381 | + for (int row = 0; row < numberOfRows; ++row) { |
| 382 | + for (int col = 0; col < numberOfColumns; ++col) { |
| 383 | + var storage = table.getColumns()[col].getStorage(); |
| 384 | + if (storage.isNothing(row)) { |
| 385 | + inserter.addNull(); |
| 386 | + } else if (storage instanceof ColumnDoubleStorage doubleStorage) { |
| 387 | + inserter.add(doubleStorage.getItemAsDouble(row)); |
| 388 | + } else if (storage instanceof ColumnLongStorage longStorage) { |
| 389 | + inserter.add(longStorage.getItemAsLong(row)); |
| 390 | + } else if (storage instanceof ColumnBooleanStorage boolStorage) { |
| 391 | + inserter.add(boolStorage.getItemAsBoolean(row)); |
| 392 | + } else { |
| 393 | + Object value = storage.getItemBoxed(row); |
| 394 | + switch (value) { |
| 395 | + case String s -> inserter.add(s); |
| 396 | + case LocalDate ld -> inserter.add(ld); |
| 397 | + case LocalTime lt -> inserter.add(lt); |
| 398 | + case ZonedDateTime zdt -> inserter.add(zdt); |
| 399 | + case BigDecimal bd -> inserter.add(bd); |
| 400 | + default -> throw new HyperUnsupportedTypeError(value.toString()); |
398 | 401 | } |
399 | 402 | } |
400 | | - inserter.endRow(); |
401 | 403 | } |
402 | | - inserter.execute(); |
| 404 | + inserter.endRow(); |
| 405 | + } |
| 406 | + inserter.execute(); |
403 | 407 | } |
404 | 408 | } |
0 commit comments