alex-plekhanov commented on code in PR #12926: URL: https://github.com/apache/ignite/pull/12926#discussion_r3040195559
########## modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SelectByKeyFieldTest.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.processors.query.calcite.integration; + +import java.util.List; +import java.util.Objects; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.calcite.CalciteQueryEngineConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.configuration.SqlConfiguration; +import org.apache.ignite.indexing.IndexingQueryEngineConfiguration; +import org.apache.ignite.internal.binary.BinaryObjectImpl; +import org.apache.ignite.internal.binary.BinaryUtils; +import org.apache.ignite.internal.processors.query.QueryUtils; +import org.apache.ignite.internal.processors.query.calcite.QueryChecker; +import org.apache.ignite.internal.util.tostring.GridToStringInclude; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.jetbrains.annotations.Nullable; +import org.junit.Ignore; +import org.junit.Test; + +import static java.util.stream.Collectors.toList; + +/** + * Checks that using {@link QueryUtils#KEY_FIELD_NAME} in condition will use + * {@link QueryUtils#PRIMARY_KEY_INDEX pk index}. + */ +public class SelectByKeyFieldTest extends AbstractBasicIntegrationTest { + /** {@inheritDoc} */ + @Override protected int nodeCount() { + return 1; + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + SqlConfiguration sqlCfg = new SqlConfiguration().setQueryEnginesConfiguration( + new CalciteQueryEngineConfiguration().setDefault(true), + new IndexingQueryEngineConfiguration() + ); + + return super.getConfiguration(igniteInstanceName) + .setSqlConfiguration(sqlCfg); + } Review Comment: Redundant method. `sql` and `assertQuery` use Calcite query engine directly. ########## modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/CacheWrappedKeyIndexImpl.java: ########## @@ -0,0 +1,119 @@ +/* + * 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.processors.query.calcite.schema; + +import java.util.List; +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.rel.RelCollation; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexLocalRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexSlot; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.util.ImmutableBitSet; +import org.apache.ignite.IgniteException; +import org.apache.ignite.internal.cache.query.index.Index; +import org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex; +import org.apache.ignite.internal.processors.query.QueryUtils; +import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext; +import org.apache.ignite.internal.processors.query.calcite.exec.IndexScan; +import org.apache.ignite.internal.processors.query.calcite.exec.IndexWrappedKeyScan; +import org.apache.ignite.internal.processors.query.calcite.exec.exp.RangeIterable; +import org.apache.ignite.internal.processors.query.calcite.metadata.ColocationGroup; +import org.apache.ignite.internal.processors.query.calcite.prepare.bounds.SearchBounds; +import org.apache.ignite.internal.processors.query.calcite.util.RexUtils; +import org.jetbrains.annotations.Nullable; + +/** Extension for column {@value QueryUtils#KEY_FIELD_NAME} in case of composite primary key. */ +class CacheWrappedKeyIndexImpl extends CacheIndexImpl { + /** */ + CacheWrappedKeyIndexImpl(RelCollation collation, String idxName, Index idx, IgniteCacheTable tbl) { + super(collation, idxName, idx, tbl); + } + + /** */ + @Override protected @Nullable List<SearchBounds> buildSearchBounds( + RelOptCluster cluster, + @Nullable RexNode cond, + RelDataType rowType, + @Nullable ImmutableBitSet requiredColumns + ) { + // Return the boundaries only if the _key = ?. + if (!matchByCondition(cond)) Review Comment: It's not quite correct: 1. It doesn't take into account required columns 2. It doesn't take into account `IS NOT DISTINCT FROM` operator Test like: ``` assertQuery("select name, age from PUBLIC.PERSON where name = ?") .withParams(name) .matches(CoreMatchers.not(QueryChecker.containsIndexScan("PUBLIC", "PERSON", QueryUtils.PRIMARY_KEY_INDEX))) .columnNames("NAME", "AGE") .returns(name, 24) .check(); ``` Fails. I propose to check bounds instead, something like: ``` if (cond != null) { List<SearchBounds> bounds = RexUtils.buildHashSearchBounds(cluster, cond, rowType, requiredColumns, true); if (bounds != null && bounds.get(QueryUtils.KEY_COL) != null) return bounds; } return null; ``` ########## modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/agg/Accumulators.java: ########## @@ -1183,6 +1192,22 @@ private ComparableMinMax( @Override public RelDataType returnType(IgniteTypeFactory typeFactory) { return typeSupplier.apply(typeFactory); } + + /** */ + @SuppressWarnings({"rawtypes", "unchecked"}) + private int compare(Object a, Object b) { + if (BinaryUtils.isBinaryObjectImpl(a) || BinaryUtils.isBinaryObjectImpl(b)) + return BinaryUtils.binariesFactory.compareForDml(a, b); Review Comment: Let's move this to Commons (maybe as two methods `isBinaryComparable(a, b)` and `compareBinary`): 1. Minimize dependency on core module classes from `Accumulators` class (to maximize compatibility with Ignite 3). 2. It's widely used in `IgniteSqlFunctions`and code will be a little bit simplier. -- 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]
