korlov42 commented on code in PR #2203:
URL: https://github.com/apache/ignite-3/pull/2203#discussion_r1520009192
##########
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++);
+ }
Review Comment:
the goal of this patch is to remove code rather than adding new one, so I
tried not add new lines until it's absolutely required in order to proceed.
There was no validation before, but after refactoring this object become more
sensible to unspoken invariants like "no duplicate columns". In order to find
and all the places that violates such invariant I've added bunch of assertions.
I think, the proper fix of validation should be done under separate issue
(https://issues.apache.org/jira/browse/IGNITE-21733).
##########
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<>();
Review Comment:
fixed, thanks
--
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]