This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a commit to branch ignite-25135-2
in repository https://gitbox.apache.org/repos/asf/ignite-3.git

commit 9182d887ea58d26e1771ecf119caa49288980256
Author: amashenkov <[email protected]>
AuthorDate: Wed Dec 24 19:11:32 2025 +0300

    Lazy SQL schema initialization.
---
 .../internal/sql/engine/schema/IgniteSchema.java   | 41 +++++------
 .../sql/engine/schema/SqlSchemaManagerImpl.java    | 80 ++++++++++++----------
 2 files changed, 61 insertions(+), 60 deletions(-)

diff --git 
a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/IgniteSchema.java
 
b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/IgniteSchema.java
index 109b1ff431a..2f62a97d6a2 100644
--- 
a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/IgniteSchema.java
+++ 
b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/IgniteSchema.java
@@ -17,16 +17,14 @@
 
 package org.apache.ignite.internal.sql.engine.schema;
 
-import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Map;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 import org.apache.calcite.schema.Table;
 import org.apache.calcite.schema.impl.AbstractSchema;
-import org.apache.ignite.internal.util.CollectionUtils;
-import org.jetbrains.annotations.Nullable;
+import org.apache.calcite.schema.lookup.Lookup;
+import org.apache.calcite.util.NameMap;
 
 /**
  * Schema implementation for sql engine.
@@ -37,16 +35,27 @@ public class IgniteSchema extends AbstractSchema {
 
     private final int catalogVersion;
 
-    private final Map<String, IgniteDataSource> tableByName;
-
-    private final Int2ObjectMap<IgniteDataSource> tableById;
+    private final Lookup<Table> tableLookup;
 
     /** Constructor. */
     public IgniteSchema(String name, int catalogVersion, Collection<? extends 
IgniteDataSource> tables) {
+       this(
+               name,
+               catalogVersion,
+               
Lookup.of(NameMap.immutableCopyOf(tables.stream().collect(Collectors.toMap(IgniteDataSource::name,
 Function.identity()))))
+       );
+    }
+
+    /** Constructor. */
+    public IgniteSchema(String name, int catalogVersion, Lookup<Table> 
tableLookup) {
         this.name = name;
         this.catalogVersion = catalogVersion;
-        this.tableByName = 
tables.stream().collect(Collectors.toMap(IgniteDataSource::name, 
Function.identity()));
-        this.tableById = 
tables.stream().collect(CollectionUtils.toIntMapCollector(IgniteDataSource::id, 
Function.identity()));
+        this.tableLookup = tableLookup;
+    }
+
+    @Override
+    public Lookup<Table> tables() {
+        return tableLookup;
     }
 
     /** Schema name. */
@@ -59,20 +68,8 @@ public class IgniteSchema extends AbstractSchema {
         return catalogVersion;
     }
 
-    /** {@inheritDoc} */
     @Override
     protected Map<String, Table> getTableMap() {
-        return Collections.unmodifiableMap(tableByName);
-    }
-
-    /** Returns table by given id. */
-    @Nullable IgniteTable tableByIdOpt(int tableId) {
-        IgniteDataSource dataSource = tableById.get(tableId);
-
-        if (!(dataSource instanceof IgniteTable)) {
-            return null;
-        }
-
-        return (IgniteTable) dataSource;
+        throw  new UnsupportedOperationException();
     }
 }
diff --git 
a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManagerImpl.java
 
b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManagerImpl.java
index a8ce4668786..57a0bc26dbb 100644
--- 
a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManagerImpl.java
+++ 
b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManagerImpl.java
@@ -23,6 +23,7 @@ import static 
org.apache.ignite.internal.util.CompletableFutures.nullCompletedFu
 import it.unimi.dsi.fastutil.objects.Object2IntMap;
 import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
@@ -39,7 +40,9 @@ import org.apache.calcite.rel.core.TableScan;
 import org.apache.calcite.rel.hint.RelHint;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.schema.SchemaPlus;
-import org.apache.calcite.schema.lookup.LikePattern;
+import org.apache.calcite.schema.Table;
+import org.apache.calcite.schema.lookup.CompatibilityLookup;
+import org.apache.calcite.schema.lookup.Lookup;
 import org.apache.calcite.tools.Frameworks;
 import org.apache.calcite.util.ImmutableIntList;
 import org.apache.ignite.internal.catalog.Catalog;
@@ -149,42 +152,43 @@ public class SqlSchemaManagerImpl implements 
SqlSchemaManager {
     @Override
     public IgniteTable table(int catalogVersion, int tableId) {
         return fullDataTableCache.get(cacheKey(catalogVersion, tableId), key 
-> {
+            // Load actual table information from the catalog.
+            Catalog catalog = catalogManager.catalog(catalogVersion);
+
+            if (catalog == null) {
+                throw new IgniteInternalException(Common.INTERNAL_ERR, 
"Catalog of given version not found: " + catalogVersion);
+            }
+
+            CatalogTableDescriptor tableDescriptor = catalog.table(tableId);
+
+            if (tableDescriptor == null) {
+                throw new IgniteInternalException(Common.INTERNAL_ERR, "Table 
with given id not found: " + tableId);
+            }
+
             IgniteSchemas rootSchema = schemaCache.get(catalogVersion);
 
             // Retrieve table from the schema (if it exists).
             if (rootSchema != null) {
                 SchemaPlus schemaPlus = rootSchema.root();
 
-                for (String name : 
schemaPlus.subSchemas().getNames(LikePattern.any())) {
-                    SchemaPlus subSchema = schemaPlus.subSchemas().get(name);
+                CatalogSchemaDescriptor schemaDescriptor = 
catalog.schema(tableDescriptor.schemaId());
 
-                    assert subSchema != null : name;
+                assert schemaDescriptor != null;
 
-                    IgniteSchema schema = subSchema.unwrap(IgniteSchema.class);
-
-                    assert schema != null : "unknown schema " + subSchema;
-
-                    // Schema contains a wrapper for IgniteTable that includes 
actual information for a table (indexes, etc).
-                    ActualIgniteTable table = (ActualIgniteTable) 
schema.tableByIdOpt(tableId);
-
-                    if (table != null) {
-                        return table;
-                    }
-                }
-            }
+                SchemaPlus subSchema = 
schemaPlus.subSchemas().get(schemaDescriptor.name());
 
-            // Load actual table information from the catalog.
+                assert subSchema != null : schemaDescriptor.name();
 
-            Catalog catalog = catalogManager.catalog(catalogVersion);
+                IgniteSchema schema = subSchema.unwrap(IgniteSchema.class);
 
-            if (catalog == null) {
-                throw new IgniteInternalException(Common.INTERNAL_ERR, 
"Catalog of given version not found: " + catalogVersion);
-            }
+                assert schema != null : "unknown schema " + subSchema;
 
-            CatalogTableDescriptor tableDescriptor = catalog.table(tableId);
+                // Schema contains a wrapper for IgniteTable that includes 
actual information for a table (indexes, etc).
+                ActualIgniteTable table = (ActualIgniteTable) 
schema.tables().get(tableDescriptor.name());
 
-            if (tableDescriptor == null) {
-                throw new IgniteInternalException(Common.INTERNAL_ERR, "Table 
with given id not found: " + tableId);
+                if (table != null) {
+                    return table;
+                }
             }
 
             CacheKey tableKey = tableCacheKey(tableDescriptor.id(), 
tableDescriptor.updateTimestamp());
@@ -228,11 +232,12 @@ public class SqlSchemaManagerImpl implements 
SqlSchemaManager {
         int catalogVersion = catalog.version();
         String schemaName = schemaDescriptor.name();
 
-        int numTables = schemaDescriptor.tables().length;
-        List<IgniteDataSource> schemaDataSources = new ArrayList<>(numTables);
+        Lookup<Table> tableLookup = new CompatibilityLookup<>(
+                schemaDescriptor::table,
+                () -> 
Arrays.stream(schemaDescriptor.tables()).map(CatalogTableDescriptor::name).collect(Collectors.toSet())
+        ).map((tableDescriptor, name) -> {
 
-        // Assemble sql-engine.TableDescriptors as they are required by 
indexes.
-        for (CatalogTableDescriptor tableDescriptor : 
schemaDescriptor.tables()) {
+            // Assemble sql-engine.TableDescriptors as they are required by 
indexes.
             CacheKey tableKey = tableCacheKey(tableDescriptor.id(), 
tableDescriptor.updateTimestamp());
 
             // Load cached table by (id, version)
@@ -247,26 +252,25 @@ public class SqlSchemaManagerImpl implements 
SqlSchemaManager {
                     tableDescriptor.primaryKeyIndexId()
             );
 
-            // Store a wrapper for the table that includes actual information 
for a table (indexes, etc),
-            // because the cached table entry (id, version) may not include 
up-to-date information on indexes.
-            schemaDataSources.add(new ActualIgniteTable(igniteTable, 
tableIndexes));
-        }
+            return new ActualIgniteTable(igniteTable, tableIndexes);
+        });
 
-        for (CatalogSystemViewDescriptor systemViewDescriptor : 
schemaDescriptor.systemViews()) {
+        Lookup<Table> systemViewLookup = new CompatibilityLookup<>(
+                schemaDescriptor::systemView,
+                () -> 
Arrays.stream(schemaDescriptor.systemViews()).map(CatalogSystemViewDescriptor::name).collect(Collectors.toSet())
+        ).map((systemViewDescriptor, name) -> {
             int viewId = systemViewDescriptor.id();
             String viewName = systemViewDescriptor.name();
             TableDescriptor descriptor = 
createTableDescriptorForSystemView(systemViewDescriptor);
 
-            IgniteSystemView schemaTable = new IgniteSystemViewImpl(
+            return new IgniteSystemViewImpl(
                     viewName,
                     viewId,
                     descriptor
             );
+        });
 
-            schemaDataSources.add(schemaTable);
-        }
-
-        return new IgniteSchema(schemaName, catalogVersion, schemaDataSources);
+        return new IgniteSchema(schemaName, catalogVersion, 
Lookup.concat(tableLookup, systemViewLookup));
     }
 
     private static IgniteIndex createSchemaIndex(

Reply via email to