Copilot commented on code in PR #6965: URL: https://github.com/apache/ignite-3/pull/6965#discussion_r2523722832
########## modules/jdbc/src/main/java/org/apache/ignite/internal/jdbc/JdbcDatabaseMetadataUtils.java: ########## @@ -0,0 +1,337 @@ +/* + * 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.ignite.internal.jdbc; + +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.EnumSet; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.UUID; +import org.apache.ignite.internal.jdbc2.ClientSyncResultSet; +import org.apache.ignite.internal.jdbc2.JdbcResultSet; +import org.apache.ignite.internal.sql.ResultSetMetadataImpl; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.internal.util.TransformingIterator; +import org.apache.ignite.sql.ColumnMetadata; +import org.apache.ignite.sql.ColumnType; +import org.apache.ignite.sql.ResultSetMetadata; +import org.apache.ignite.sql.SqlRow; +import org.apache.ignite.table.Tuple; +import org.jetbrains.annotations.Nullable; + +/** + * Helper methods for creating a {@link ResultSet} using a list of objects. + */ +class JdbcDatabaseMetadataUtils { + /** List of supported metadata column types. */ + private static final EnumSet<ColumnType> SUPPORTED_TYPES = EnumSet.of( + ColumnType.NULL, + ColumnType.BOOLEAN, + ColumnType.INT8, + ColumnType.INT16, + ColumnType.INT32, + ColumnType.INT64, + ColumnType.STRING + ); + + /** Creates an empty {@link ResultSet} using the provided metadata. */ + static ResultSet createObjectListResultSet(List<ColumnMetadata> columnsMeta) { + return createObjectListResultSet(List.of(), columnsMeta); + } + + /** Creates a {@link ResultSet} using the provided list of objects and metadata. */ + static ResultSet createObjectListResultSet(List<List<Object>> rows, List<ColumnMetadata> columnsMeta) { + ResultSetMetadata meta = new ResultSetMetadataImpl(columnsMeta); + + if (IgniteUtils.assertionsEnabled()) { + for (ColumnMetadata columnMeta : meta.columns()) { + assert SUPPORTED_TYPES.contains(columnMeta.type()) : "Unsupported column type: " + columnMeta.type(); + } + } + + TransformingIterator<List<Object>, SqlRow> transformer = + new TransformingIterator<>(rows.iterator(), ObjectListToSqlRowAdapter::new); + + return new JdbcResultSet( + new IteratorBasedClientSyncResultSet(transformer, meta), + null, + null, + false, + 0 + ); Review Comment: Passing `null` for both `statement` and `zoneIdSupplier` parameters can cause `NullPointerException` in the JdbcResultSet methods: 1. `isClosed()` calls `statement.isClosed()` without null check (JdbcResultSet line 1899) 2. `instantWithLocalTimeZone()` calls `zoneIdSupplier.get()` without null check (JdbcResultSet line 2258) While timestamp types aren't in SUPPORTED_TYPES, the statement issue will definitely cause problems. Consider passing non-null values or ensuring JdbcResultSet handles null parameters properly. ########## modules/jdbc/src/main/java/org/apache/ignite/internal/jdbc2/JdbcResultSet.java: ########## @@ -105,7 +105,8 @@ public class JdbcResultSet implements ResultSet { /** * Constructor. */ - JdbcResultSet( + // TODO https://issues.apache.org/jira/browse/IGNITE-26145 Remove "public" modifier + public JdbcResultSet( Review Comment: Making this constructor public allows it to be called with `null` for the `statement` parameter (as done in `JdbcDatabaseMetadataUtils`). However, this can cause a `NullPointerException` in `isClosed()` method (line 1899), which calls `statement.isClosed()` without null-checking. Consider either: 1. Documenting that statement can be null and fixing usages accordingly 2. Adding validation to prevent null statement parameter 3. Keeping the constructor package-private and finding an alternative approach ########## modules/jdbc/src/main/java/org/apache/ignite/internal/jdbc/JdbcDatabaseMetadataUtils.java: ########## @@ -0,0 +1,337 @@ +/* + * 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.ignite.internal.jdbc; + +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.EnumSet; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.UUID; +import org.apache.ignite.internal.jdbc2.ClientSyncResultSet; +import org.apache.ignite.internal.jdbc2.JdbcResultSet; +import org.apache.ignite.internal.sql.ResultSetMetadataImpl; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.internal.util.TransformingIterator; +import org.apache.ignite.sql.ColumnMetadata; +import org.apache.ignite.sql.ColumnType; +import org.apache.ignite.sql.ResultSetMetadata; +import org.apache.ignite.sql.SqlRow; +import org.apache.ignite.table.Tuple; +import org.jetbrains.annotations.Nullable; + +/** + * Helper methods for creating a {@link ResultSet} using a list of objects. + */ +class JdbcDatabaseMetadataUtils { + /** List of supported metadata column types. */ + private static final EnumSet<ColumnType> SUPPORTED_TYPES = EnumSet.of( + ColumnType.NULL, + ColumnType.BOOLEAN, + ColumnType.INT8, + ColumnType.INT16, + ColumnType.INT32, + ColumnType.INT64, + ColumnType.STRING + ); + + /** Creates an empty {@link ResultSet} using the provided metadata. */ + static ResultSet createObjectListResultSet(List<ColumnMetadata> columnsMeta) { + return createObjectListResultSet(List.of(), columnsMeta); + } + + /** Creates a {@link ResultSet} using the provided list of objects and metadata. */ + static ResultSet createObjectListResultSet(List<List<Object>> rows, List<ColumnMetadata> columnsMeta) { + ResultSetMetadata meta = new ResultSetMetadataImpl(columnsMeta); + + if (IgniteUtils.assertionsEnabled()) { + for (ColumnMetadata columnMeta : meta.columns()) { + assert SUPPORTED_TYPES.contains(columnMeta.type()) : "Unsupported column type: " + columnMeta.type(); + } + } + + TransformingIterator<List<Object>, SqlRow> transformer = + new TransformingIterator<>(rows.iterator(), ObjectListToSqlRowAdapter::new); + + return new JdbcResultSet( + new IteratorBasedClientSyncResultSet(transformer, meta), + null, + null, + false, + 0 + ); + } + + private static class IteratorBasedClientSyncResultSet implements ClientSyncResultSet { + private final ResultSetMetadata metadata; + private final Iterator<SqlRow> rowsIterator; + + IteratorBasedClientSyncResultSet(Iterator<SqlRow> rowsIterator, ResultSetMetadata metadata) { + this.rowsIterator = rowsIterator; + this.metadata = metadata; + } + + @Override + public ResultSetMetadata metadata() { + return metadata; + } + + @Override + public boolean hasRowSet() { + return true; + } + + @Override + public long affectedRows() { + return -1; + } + + @Override + public boolean wasApplied() { + return false; + } + + @Override + public boolean hasNextResultSet() { + return false; + } + + @Override + public ClientSyncResultSet nextResultSet() { + throw new NoSuchElementException("Query has no more results"); + } + + @Override + public void close() { + // No-op. + } + + @Override + public boolean hasNext() { + return rowsIterator.hasNext(); + } + + @Override + public SqlRow next() { + return rowsIterator.next(); + } + } + + private static class ObjectListToSqlRowAdapter implements SqlRow { + private final List<Object> row; + + ObjectListToSqlRowAdapter(List<Object> row) { + this.row = row; + } + + @Override Review Comment: This unchecked cast could cause `ClassCastException` at runtime if the stored object type doesn't match the requested type T. Consider adding a `@SuppressWarnings("unchecked")` annotation to acknowledge this is intentional, since the caller is responsible for requesting the correct type based on metadata. ```suggestion @Override @SuppressWarnings("unchecked") ``` -- 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]
