Skip to content

Commit 4a218ad

Browse files
committed
fix hana insertion
1 parent e301226 commit 4a218ad

3 files changed

Lines changed: 37 additions & 18 deletions

File tree

backend/src/main/java/com/bakdata/conquery/models/datasets/concepts/conditions/ColumnEqualCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public WhereCondition convertToSqlCondition(CTConditionContext context) {
5858

5959
@Override
6060
public Expression buildExpression(CTConditionContext context, ConceptElement<?> id) {
61-
return new Expression(id, Map.of(field(name(getColumn()), VARCHAR).as("%s_equal".formatted(column)), values.stream().map(DSL::val).collect(Collectors.toSet())));
61+
return new Expression(id, Map.of(field(name(getColumn()), VARCHAR(32)).as("%s_equal".formatted(column)), values.stream().map(DSL::val).collect(Collectors.toSet())));
6262

6363
}
6464
}

backend/src/main/java/com/bakdata/conquery/models/datasets/concepts/conditions/EqualCondition.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.bakdata.conquery.util.CalculatedValue;
1616
import com.bakdata.conquery.util.CollectionsUtil;
1717
import com.fasterxml.jackson.annotation.JsonCreator;
18+
import com.google.common.base.Preconditions;
1819
import lombok.AllArgsConstructor;
1920
import lombok.Getter;
2021
import lombok.Setter;
@@ -49,8 +50,6 @@ public WhereCondition convertToSqlCondition(CTConditionContext context) {
4950

5051
@Override
5152
public Expression buildExpression(CTConditionContext context, ConceptElement<?> id) {
52-
return new Expression(id,
53-
Map.of(context.getConnectorColumn(), values.stream().map(DSL::val).collect(Collectors.toSet()))
54-
);
53+
return new Expression(id, Map.of(context.getConnectorColumn(), values.stream().map(DSL::val).collect(Collectors.toSet())));
5554
}
5655
}

backend/src/main/java/com/bakdata/conquery/sql/conquery/SqlMatchingStats.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.jooq.SelectConditionStep;
5050
import org.jooq.SelectJoinStep;
5151
import org.jooq.Table;
52+
import org.jooq.exception.DataAccessException;
5253

5354
@Slf4j
5455
@Data
@@ -79,7 +80,10 @@ private static void assignStatsToPath(ConceptElementId<?> resolvedId, Map<Concep
7980
* collect unique fields used/defined in the expressions.
8081
*/
8182
private static List<Field<?>> collectAllFields(List<CTCondition.Expression> expressions) {
83+
84+
8285
List<Field<?>> fields = expressions.stream()
86+
//TODO determine length of chars, for now we are relying on a fixed length because it's quite cumbersome
8387
.map(expression -> expression.conditions().keySet())
8488
.flatMap(Collection::stream)
8589
.distinct()
@@ -119,11 +123,13 @@ public void createConceptIdJoinTable(TreeConcept concept) {
119123
Name tableName = idsTableName(concept.getName());
120124

121125
// allFields are the statements to extract values from the underlying tables, we use them to generate the field names
122-
List<Field<?>> fieldNames = new ArrayList<>(allFields);
123-
fieldNames.addFirst(field(CONCEPT_ID_FIELD.getName(), VARCHAR(findMaxIdLength(expressions))));
126+
List<Field<?>> fields = new ArrayList<>();
127+
128+
fields.addAll(allFields);
129+
fields.addFirst(field(CONCEPT_ID_FIELD.getName(), VARCHAR(findMaxIdLength(expressions))));
124130

125-
createConceptIdsTable(tableName, fieldNames);
126-
insertConceptIdMappings(tableName, fieldNames, rows, dslContext);
131+
createConceptIdsTable(tableName, fields);
132+
insertConceptIdMappings(tableName, fields, rows, dslContext);
127133
}
128134

129135
@NotNull
@@ -211,11 +217,19 @@ private Name idsTableName(@NotBlank String name) {
211217
private void insertConceptIdMappings(Name tableName, List<Field<?>> fieldNames, List<RowN> rows, DSLContext dsl) {
212218
log.info("BEGIN inserting {} rows into {}", rows.size(), tableName);
213219

214-
InsertValuesStepN<Record> insertConceptTable = dsl.insertInto(table(tableName))
215-
.columns(fieldNames)
216-
.valuesOfRows(rows);
220+
// We're using batching here because some DBMS don't allow mass inserts.
221+
// There's a chance, we rework this to use a prepared statement with lots of bindings under the hood. But that needs to rework the entire stream of rows.
222+
List<InsertValuesStepN<?>> inserts = new ArrayList<>(rows.size());
223+
224+
for (RowN row : rows) {
225+
inserts.add(dsl.insertInto(table(tableName))
226+
.columns(fieldNames)
227+
.values(row));
228+
}
229+
230+
dsl.batch(inserts)
231+
.execute();
217232

218-
insertConceptTable.execute();
219233

220234
log.trace("DONE inserting into {}", tableName);
221235
}
@@ -224,17 +238,23 @@ private void insertConceptIdMappings(Name tableName, List<Field<?>> fieldNames,
224238
* Drop the table, then recreate it.
225239
* TODO add an index.
226240
*/
227-
private void createConceptIdsTable(Name tableName, List<Field<?>> fieldNames) {
241+
private void createConceptIdsTable(Name tableName, List<Field<?>> fields) {
228242

229-
log.debug("Creating table {} with fields {}", tableName, fieldNames);
243+
log.debug("Creating table {} with fields {}", tableName, fields);
230244

231-
dslContext.dropTableIfExists(tableName)
232-
.cascade()
233-
.execute();
245+
try {
246+
dslContext.dropTable(tableName)
247+
.cascade()
248+
.execute();
249+
}
250+
catch (DataAccessException exception) {
251+
// Likely it doesn't exist. Some DBMS just don't support drop-IfExists so this is the next best thing :^)
252+
log.trace("Failed to drop table {}", tableName, exception);
253+
}
234254

235255
CreateTableElementListStep createTable =
236256
dslContext.createTable(tableName)
237-
.columns(fieldNames);
257+
.columns(fields);
238258

239259

240260
createTable.execute();

0 commit comments

Comments
 (0)