gianm commented on code in PR #19711:
URL: https://github.com/apache/druid/pull/19711#discussion_r3636049211


##########
server/src/main/java/org/apache/druid/catalog/model/ClusteredValueGroupsBaseTableMetadata.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.catalog.model;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import 
org.apache.druid.data.input.impl.ClusteredValueGroupsBaseTableProjectionSpec;
+import org.apache.druid.data.input.impl.DimensionSchema;
+import org.apache.druid.error.InvalidInput;
+import org.apache.druid.segment.NestedDataColumnSchema;
+import org.apache.druid.segment.VirtualColumns;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.utils.CollectionUtils;
+
+import javax.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Catalog layout metadata for {@link 
ClusteredValueGroupsBaseTableProjectionSpec} base tables. Declares the
+ * {@link #clusteringColumns} (names of declared catalog columns that rows are 
clustered by) and optional
+ * {@link #virtualColumns} that compute stored columns at ingest time; 
everything else about the physical spec
+ * (column names, types, and order) is taken from the catalog column list by 
{@link #createSpec(List)}. The declared
+ * column order is the physical segment order, so, mirroring the physical 
spec, the clustering columns must be
+ * declared as the leading prefix of the column list.
+ */
+@JsonTypeName(ClusteredValueGroupsBaseTableMetadata.TYPE_NAME)
+public class ClusteredValueGroupsBaseTableMetadata implements 
DatasourceBaseTableMetadata
+{
+  public static final String TYPE_NAME = 
ClusteredValueGroupsBaseTableProjectionSpec.TYPE_NAME;
+
+  private final List<String> clusteringColumns;
+  private final VirtualColumns virtualColumns;
+
+  @JsonCreator
+  public ClusteredValueGroupsBaseTableMetadata(
+      @JsonProperty("clusteringColumns") List<String> clusteringColumns,
+      @JsonProperty("virtualColumns") @Nullable VirtualColumns virtualColumns
+  )
+  {
+    this.clusteringColumns = clusteringColumns == null ? 
Collections.emptyList() : clusteringColumns;
+    this.virtualColumns = virtualColumns == null ? VirtualColumns.EMPTY : 
virtualColumns;
+  }
+
+  @Override
+  @JsonProperty("type")
+  public String getType()
+  {
+    return TYPE_NAME;
+  }
+
+  @JsonProperty("clusteringColumns")
+  public List<String> getClusteringColumns()
+  {
+    return clusteringColumns;
+  }
+
+  @Override
+  @JsonProperty("virtualColumns")
+  @JsonInclude(JsonInclude.Include.NON_DEFAULT)
+  public VirtualColumns getVirtualColumns()
+  {
+    return virtualColumns;
+  }
+
+  /**
+   * Creates the physical spec from the declared catalog columns, used 
verbatim: the declared column order is the
+   * physical segment order, so the clustering columns must be declared as the 
leading prefix of the column list (in
+   * {@link #clusteringColumns} order) — anything else is a validation error, 
mirroring the physical spec. Column
+   * types come from the catalog column types ({@link Columns#druidType}; 
untyped columns default to STRING, mirroring
+   * {@link Columns#convertSignature}); the declared type is retained in the 
ingestion schema (primitive arrays cast
+   * an auto column to the declared type, {@code COMPLEX<json>} uses the 
nested column schema, and other complex
+   * types are rejected — clustered base tables have no aggregators to produce 
them). Every clustering column must be
+   * a declared column — a clustering column
+   * computed by a virtual column at ingest time is still a stored, queryable 
column, so it too must appear in the
+   * column list. All ordering and layout rules (leading prefix, allowed 
clustering types, explicit non-clustering
+   * {@code __time}, no duplicates) are enforced by the spec itself.
+   */
+  @Override
+  public ClusteredValueGroupsBaseTableProjectionSpec 
createSpec(List<ColumnSpec> columns)
+  {
+    if (CollectionUtils.isNullOrEmpty(columns)) {
+      throw InvalidInput.exception(
+          "Cannot define a [%s] base table without declared columns; the 
catalog column list defines the table schema",
+          TYPE_NAME
+      );
+    }
+    final Set<String> declaredNames = new HashSet<>();
+    final List<DimensionSchema> specColumns = new ArrayList<>(columns.size());
+    for (ColumnSpec column : columns) {
+      declaredNames.add(column.name());
+      specColumns.add(toDimensionSchema(column));
+    }
+    for (String clusteringColumn : clusteringColumns) {
+      if (!declaredNames.contains(clusteringColumn)) {
+        throw InvalidInput.exception(
+            "clustering column [%s] is not a declared column; clustering 
columns must be declared as the leading"
+            + " prefix of the table's column list, including columns computed 
by a virtual column at ingest time"
+            + " (they are stored columns)",
+            clusteringColumn
+        );
+      }
+    }
+    return ClusteredValueGroupsBaseTableProjectionSpec.builder()
+                                                      
.virtualColumns(virtualColumns)
+                                                      .columns(specColumns)
+                                                      
.clusteringColumns(clusteringColumns)
+                                                      .build();
+  }
+
+  private static DimensionSchema toDimensionSchema(ColumnSpec column)
+  {
+    ColumnType druidType = Columns.druidType(column);
+    if (druidType == null) {
+      druidType = ColumnType.STRING;
+    }
+    if (druidType.isPrimitive() || druidType.isPrimitiveArray()) {
+      // The declared type is retained in the ingestion schema (primitive 
arrays are cast, rather than left to an
+      // untyped auto column whose type is inferred from the ingested values; 
note that the auto schema stores
+      // FLOAT ARRAY as DOUBLE ARRAY).
+      return DimensionSchema.getDefaultSchemaForBuiltInType(column.name(), 
druidType);
+    }
+    if (ColumnType.NESTED_DATA.equals(druidType)) {
+      return new NestedDataColumnSchema(column.name(), 5);

Review Comment:
   Can this 5 be a static constant?



##########
server/src/main/java/org/apache/druid/catalog/model/facade/DatasourceFacade.java:
##########
@@ -195,4 +196,24 @@ public List<DatasourceProjectionMetadata> projections()
       return null;
     }
   }
+
+  @Nullable
+  public DatasourceBaseTableMetadata baseTableMetadata()
+  {
+    Object value = property(DatasourceDefn.BASE_TABLE_PROPERTY);
+    if (value == null) {
+      return null;
+    }
+    try {
+      return jsonMapper().convertValue(value, 
DatasourceDefn.BaseTableDefn.TYPE_REF);
+    }
+    catch (Exception e) {
+      LOG.error(

Review Comment:
   Include `e` in the log. Wrap the `%s` in `[%s]` per log message style.



##########
server/src/main/java/org/apache/druid/catalog/model/table/DatasourceDefn.java:
##########
@@ -85,12 +95,37 @@ public DatasourceDefn()
             new ClusterKeysDefn(),
             new HiddenColumnsDefn(),
             new ModelProperties.BooleanPropertyDefn(SEALED_PROPERTY),
-            new ProjectionsDefn()
+            new ProjectionsDefn(),
+            new BaseTableDefn()
         ),
         null
     );
   }
 
+  @Override
+  public void validate(ResolvedTable table)
+  {
+    super.validate(table);
+    final DatasourceBaseTableMetadata baseTable = 
table.decodeProperty(BASE_TABLE_PROPERTY);
+    if (baseTable != null) {
+      // A base table layout derives the physical segment schema from the 
declared columns, so a column the query
+      // produces but the table does not declare cannot be stored; require 
'sealed' so ingestion rejects such columns
+      // instead of silently dropping them. Requiring the flag (rather than 
implying strictness) reserves the

Review Comment:
   The wording "reserves the non-sealed state" seems awkward; what's it trying 
to say? I don't think I understand what this last sentence means.



##########
server/src/main/java/org/apache/druid/catalog/model/ClusteredValueGroupsBaseTableMetadata.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.catalog.model;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import 
org.apache.druid.data.input.impl.ClusteredValueGroupsBaseTableProjectionSpec;
+import org.apache.druid.data.input.impl.DimensionSchema;
+import org.apache.druid.error.InvalidInput;
+import org.apache.druid.segment.NestedDataColumnSchema;
+import org.apache.druid.segment.VirtualColumns;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.utils.CollectionUtils;
+
+import javax.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Catalog layout metadata for {@link 
ClusteredValueGroupsBaseTableProjectionSpec} base tables. Declares the
+ * {@link #clusteringColumns} (names of declared catalog columns that rows are 
clustered by) and optional
+ * {@link #virtualColumns} that compute stored columns at ingest time; 
everything else about the physical spec
+ * (column names, types, and order) is taken from the catalog column list by 
{@link #createSpec(List)}. The declared
+ * column order is the physical segment order, so, mirroring the physical 
spec, the clustering columns must be
+ * declared as the leading prefix of the column list.
+ */
+@JsonTypeName(ClusteredValueGroupsBaseTableMetadata.TYPE_NAME)
+public class ClusteredValueGroupsBaseTableMetadata implements 
DatasourceBaseTableMetadata
+{
+  public static final String TYPE_NAME = 
ClusteredValueGroupsBaseTableProjectionSpec.TYPE_NAME;
+
+  private final List<String> clusteringColumns;
+  private final VirtualColumns virtualColumns;
+
+  @JsonCreator
+  public ClusteredValueGroupsBaseTableMetadata(
+      @JsonProperty("clusteringColumns") List<String> clusteringColumns,
+      @JsonProperty("virtualColumns") @Nullable VirtualColumns virtualColumns
+  )
+  {
+    this.clusteringColumns = clusteringColumns == null ? 
Collections.emptyList() : clusteringColumns;
+    this.virtualColumns = virtualColumns == null ? VirtualColumns.EMPTY : 
virtualColumns;
+  }
+
+  @Override
+  @JsonProperty("type")
+  public String getType()
+  {
+    return TYPE_NAME;
+  }
+
+  @JsonProperty("clusteringColumns")
+  public List<String> getClusteringColumns()
+  {
+    return clusteringColumns;
+  }
+
+  @Override
+  @JsonProperty("virtualColumns")
+  @JsonInclude(JsonInclude.Include.NON_DEFAULT)
+  public VirtualColumns getVirtualColumns()
+  {
+    return virtualColumns;
+  }
+
+  /**
+   * Creates the physical spec from the declared catalog columns, used 
verbatim: the declared column order is the

Review Comment:
   This javadoc is written too densely and could use a reword.



##########
embedded-tests/src/test/java/org/apache/druid/testing/embedded/catalog/CatalogIngestAndQueryTest.java:
##########
@@ -415,6 +423,123 @@ public void testInsertWithMultiClusteringFromQuery()
     );
   }
 
+  /**
+   * Create a table with columns (in declared order, which is the physical 
segment order — the clustering column
+   * must be declared first):

Review Comment:
   What happens if it isn't?



##########
processing/src/main/java/org/apache/druid/data/input/impl/DimensionSchema.java:
##########
@@ -90,6 +91,12 @@ public static DimensionSchema 
getDefaultSchemaForBuiltInType(String name, TypeSi
       case DOUBLE:
         return new DoubleDimensionSchema(name);
       default:
+        if (type.isPrimitiveArray()) {
+          // An untyped auto column infers its physical type from the ingested 
values, discarding the known type

Review Comment:
   This comment doesn't need to be talking about discarding the known type and 
FLOAT and DOUBLE arrays. It could be, like:
   
   ```java
   // Use the auto column with type coercion when dealing with arrays of 
primitive types.
   // Otherwise, allow type inference, which may yield a column of a different 
type. 
   ```
   



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to