AMashenkov commented on code in PR #2203:
URL: https://github.com/apache/ignite-3/pull/2203#discussion_r1519835644
##########
modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaDescriptor.java:
##########
@@ -84,71 +82,118 @@ public class SchemaDescriptor {
* @param keyCols Key columns.
* @param valCols Value columns.
*/
+ @TestOnly
public SchemaDescriptor(int ver, Column[] keyCols, Column[] valCols) {
- this(ver, keyCols, null, valCols);
+ this(
+ ver,
+ mergeColumns(keyCols, valCols),
+
Arrays.stream(keyCols).map(Column::name).collect(Collectors.toList()),
+ null
+ );
}
- /**
- * Constructor.
- *
- * @param ver Schema version.
- * @param keyCols Key columns.
- * @param colocationCols Colocation column names.
- * @param valCols Value columns.
- */
- public SchemaDescriptor(int ver, Column[] keyCols, String @Nullable[]
colocationCols, Column[] valCols) {
- assert keyCols.length > 0 : "No key columns are configured.";
+ /** Constructor. */
+ public SchemaDescriptor(
+ int ver,
+ List<Column> columns,
+ List<String> keyColumns,
+ @Nullable List<String> colocationColumns
+ ) {
+ assert !nullOrEmpty(columns) : "Schema should have at least one
column";
+
+ Map<String, Column> columnsByName = new HashMap<>();
+ List<Column> orderedColumns = new ArrayList<>(columns.size());
+
+ Object2IntMap<String> columnNameToPositionInKey = new
Object2IntOpenHashMap<>();
+ int idx = 0;
+ for (String name : keyColumns) {
+ assert !columnNameToPositionInKey.containsKey(name)
+ : "Key column must not have duplicates: " + name;
+
+ columnNameToPositionInKey.put(name, idx++);
+ }
- this.ver = ver;
- this.keyCols = new Columns(0, keyCols);
- this.valCols = new Columns(keyCols.length, valCols);
-
- this.columns = Stream.concat(
- Arrays.stream(this.keyCols.columns()),
- Arrays.stream(this.valCols.columns())
- ).collect(Collectors.toList());
-
- assert this.keyCols.nullMapSize() == 0 : "Primary key cannot contain
nullable column [cols=" + this.keyCols + ']';
-
- colMap = newLinkedHashMap(keyCols.length + valCols.length);
- var hasTemporalColumns = new AtomicBoolean(false);
-
- Stream.concat(Arrays.stream(this.keyCols.columns()),
Arrays.stream(this.valCols.columns()))
- .sorted(Comparator.comparingInt(Column::columnOrder))
- .forEach(c -> {
- if (c.type() instanceof TemporalNativeType) {
- hasTemporalColumns.set(true);
- }
-
- colMap.put(c.name(), c);
- });
-
- this.physicalOrderMatchesLogical =
colMap.values().stream().allMatch(col -> col.columnOrder() ==
col.schemaIndex());
-
- this.hasTemporalColumns = hasTemporalColumns.get();
-
- // Preserving key chunk column order is not actually required.
- // It is sufficient to has same column order for all nodes.
- if (ArrayUtils.nullOrEmpty(colocationCols)) {
- this.colocationCols = this.keyCols.columns();
- this.colocationColIndexes = null;
- } else {
- this.colocationCols = new Column[colocationCols.length];
- this.colocationColIndexes = new HashMap<>(colocationCols.length);
-
- for (int i = 0; i < colocationCols.length; i++) {
- Column col = colMap.get(colocationCols[i]);
- this.colocationCols[i] = col;
- this.colocationColIndexes.put(col, i);
+ if (colocationColumns == null) {
+ colocationColumns = keyColumns;
+ }
+
+ Object2IntMap<String> columnNameToPositionInColocation = new
Object2IntOpenHashMap<>();
+ idx = 0;
+ for (String name : colocationColumns) {
+ assert columnNameToPositionInKey.containsKey(name)
+ : "Colocation column must be part of the key: " + name;
+ assert !columnNameToPositionInColocation.containsKey(name)
+ : "Colocation column must not have duplicates: " + name;
+
+ columnNameToPositionInColocation.put(name, idx++);
+ }
+
+ boolean hasTemporalColumns = false;
+ int rowPosition = 0;
+ int valuePosition = 0;
+ for (Column column : columns) {
+ Column orderedColumn = column.copy(
+ rowPosition++,
+ columnNameToPositionInKey.getOrDefault(column.name(), -1),
+ columnNameToPositionInKey.containsKey(column.name()) ? -1
: valuePosition++,
+
columnNameToPositionInColocation.getOrDefault(column.name(), -1)
+ );
+
+ Column old = columnsByName.put(orderedColumn.name(),
orderedColumn);
+
+ assert old == null : "Columns with similar names are not allowed:
" + old.name();
+
+ orderedColumns.add(orderedColumn);
+
+ if (column.type() instanceof TemporalNativeType) {
+ hasTemporalColumns = true;
}
}
- // TODO: Move keyIndex and colocationIndex to Column class for faster
and simpler access?
- keyColIndexes = new HashMap<>(keyCols.length);
+ this.ver = ver;
+ this.columns = List.copyOf(orderedColumns);
+ this.columnsByName = Map.copyOf(columnsByName);
Review Comment:
But it's create a copy of collection.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]