korlov42 commented on code in PR #5145: URL: https://github.com/apache/ignite-3/pull/5145#discussion_r1942738701
########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/datatypes/utils/BinaryPair.java: ########## @@ -0,0 +1,48 @@ +/* + * 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.sql.engine.planner.datatypes.utils; + +import org.apache.ignite.internal.type.NativeType; + +/** + * Enumerates possible binary type pairs for test purposes. + */ +public enum BinaryPair implements TypePair { + VARBINARY1_VARBINARY2(Types.VARBINARY_1, Types.VARBINARY_2), + VARBINARY2_VARBINARY1(Types.VARBINARY_2, Types.VARBINARY_1), + VARBINARY1_VARBINARY1(Types.VARBINARY_1, Types.VARBINARY_1), + VARBINARY_VARBINARY2(Types.VARBINARY, Types.VARBINARY_2); Review Comment: ```suggestion VARBINARY_1_VARBINARY_2(Types.VARBINARY_1, Types.VARBINARY_2), VARBINARY_2_VARBINARY_1(Types.VARBINARY_2, Types.VARBINARY_1), VARBINARY_1_VARBINARY_1(Types.VARBINARY_1, Types.VARBINARY_1), VARBINARY_VARBINARY_2(Types.VARBINARY, Types.VARBINARY_2); ``` ########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/datatypes/BinaryInsertCoercionTest.java: ########## @@ -0,0 +1,159 @@ +/* + * 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.sql.engine.planner.datatypes; + +import static org.apache.calcite.sql.type.SqlTypeName.BINARY_TYPES; +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; +import static org.apache.ignite.internal.sql.engine.util.TypeUtils.native2relationalType; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import java.util.stream.Stream; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlKind; +import org.apache.ignite.internal.sql.engine.planner.datatypes.utils.BinaryPair; +import org.apache.ignite.internal.sql.engine.planner.datatypes.utils.TypePair; +import org.apache.ignite.internal.sql.engine.planner.datatypes.utils.Types; +import org.apache.ignite.internal.sql.engine.rel.IgniteKeyValueModify; +import org.apache.ignite.internal.sql.engine.rel.IgniteRel; +import org.apache.ignite.internal.sql.engine.schema.IgniteSchema; +import org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory; +import org.apache.ignite.internal.sql.engine.util.Commons; +import org.apache.ignite.internal.type.NativeType; +import org.apache.ignite.internal.type.VarlenNativeType; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * A set of tests to verify behavior of type coercion for INSERT operations, when values belongs to the BINARY type family. + * + * <p>This tests aim to help to understand in which cases implicit cast will be added to which values. + */ +public class BinaryInsertCoercionTest extends BaseTypeCoercionTest { + @ParameterizedTest + @MethodSource("args") + public void insert( + TypePair pair, + Matcher<RexNode> operandMatcher + ) throws Exception { + IgniteSchema schema = createSchemaWithSingleColumnTable(pair.first()); + + int length = ((VarlenNativeType) pair.second()).length(); + + String byteVal = "x'" + "01".repeat(length) + "'"; + + assertPlan("INSERT INTO T VALUES(" + byteVal + ")", schema, keyValOperandMatcher(operandMatcher)::matches, List.of()); + } + + @ParameterizedTest + @MethodSource("argsDyn") + public void insertDynamicParameters( + TypePair pair, + Matcher<RexNode> operandMatcher + ) throws Exception { + IgniteSchema schema = createSchemaWithSingleColumnTable(pair.first()); + + int length = ((VarlenNativeType) pair.second()).length(); + + byte[] byteVal = new byte[length]; + + assertPlan("INSERT INTO T VALUES(?)", schema, keyValOperandMatcher(operandMatcher)::matches, List.of(byteVal)); + } + Review Comment: please add test to make sure we haven't forgotten any type pair. See `org.apache.ignite.internal.sql.engine.planner.datatypes.NumericInsertSourcesCoercionTest#insertArgsIncludesAllTypePairs` for example ########## modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDynamicParameterTest.java: ########## @@ -235,6 +236,23 @@ public void testWithDifferentParametersTypes() { assertQuery("SELECT UPPER(TYPEOF(?))").withParams(1d).returns("DOUBLE").check(); } + /** Check that insertion value is trimmed if leading is zeroes. */ + @ParameterizedTest + @CsvSource({"BINARY", "VARBINARY"}) + public void testInsertZeroContainedBinary(String type) { + sql(format("CREATE TABLE t(id INT PRIMARY KEY, val {}(5000))", type)); Review Comment: I would suggest to create table with 3 columns instead of creating 2 tables with 2 columns ########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/datatypes/utils/Types.java: ########## @@ -198,6 +200,10 @@ public final class Types { public static final NativeType VARCHAR_128 = NativeTypes.stringOf(128); public static final NativeType VARCHAR_DEFAULT = NativeTypes.stringOf(-1); + public static final NativeType VARBINARY = NativeTypes.blobOf(PRECISION_NOT_SPECIFIED); Review Comment: this in fact not quite correct type. `PRECISION_NOT_SPECIFIED ` constant is used to represent user's input, like `CAST(123 AS VARCHAR)`, but after validation phase such types must have valid precision. Besides, negative precision can't be used to create table, and IGNITE-24153 introduces such validation ########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/datatypes/BinaryInsertCoercionTest.java: ########## @@ -0,0 +1,159 @@ +/* + * 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.sql.engine.planner.datatypes; + +import static org.apache.calcite.sql.type.SqlTypeName.BINARY_TYPES; +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; +import static org.apache.ignite.internal.sql.engine.util.TypeUtils.native2relationalType; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import java.util.stream.Stream; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlKind; +import org.apache.ignite.internal.sql.engine.planner.datatypes.utils.BinaryPair; +import org.apache.ignite.internal.sql.engine.planner.datatypes.utils.TypePair; +import org.apache.ignite.internal.sql.engine.planner.datatypes.utils.Types; +import org.apache.ignite.internal.sql.engine.rel.IgniteKeyValueModify; +import org.apache.ignite.internal.sql.engine.rel.IgniteRel; +import org.apache.ignite.internal.sql.engine.schema.IgniteSchema; +import org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory; +import org.apache.ignite.internal.sql.engine.util.Commons; +import org.apache.ignite.internal.type.NativeType; +import org.apache.ignite.internal.type.VarlenNativeType; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * A set of tests to verify behavior of type coercion for INSERT operations, when values belongs to the BINARY type family. + * + * <p>This tests aim to help to understand in which cases implicit cast will be added to which values. + */ +public class BinaryInsertCoercionTest extends BaseTypeCoercionTest { + @ParameterizedTest + @MethodSource("args") + public void insert( + TypePair pair, + Matcher<RexNode> operandMatcher + ) throws Exception { + IgniteSchema schema = createSchemaWithSingleColumnTable(pair.first()); + + int length = ((VarlenNativeType) pair.second()).length(); + + String byteVal = "x'" + "01".repeat(length) + "'"; + + assertPlan("INSERT INTO T VALUES(" + byteVal + ")", schema, keyValOperandMatcher(operandMatcher)::matches, List.of()); + } + + @ParameterizedTest + @MethodSource("argsDyn") + public void insertDynamicParameters( + TypePair pair, + Matcher<RexNode> operandMatcher + ) throws Exception { + IgniteSchema schema = createSchemaWithSingleColumnTable(pair.first()); + + int length = ((VarlenNativeType) pair.second()).length(); + + byte[] byteVal = new byte[length]; Review Comment: same, why don't you use utility method here? ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/TypeUtils.java: ########## @@ -733,41 +745,68 @@ public static <RowT> RowT validateCharactersOverflowAndTrimIfPossible( for (int i = 0; i < colCount; ++i) { RelDataType colType = rowType.getFieldList().get(i).getType(); + SqlTypeName typeName = colType.getSqlTypeName(); Object data = rowHandler.get(i, row); - // Skip null values and non-character types. - if (!CHAR_TYPES.contains(colType.getSqlTypeName()) || data == null) { + if (data == null || (!BINARY_TYPES.contains(typeName) && !CHAR_TYPES.contains(typeName))) { if (rowBldr != null) { rowBldr.addField(data); } continue; } - // Otherwise validate and trim if needed. - assert data instanceof String; + int colPrecision = colType.getPrecision(); - String str = (String) data; + // Validate and trim if needed. - int colPrecision = colType.getPrecision(); + if (BINARY_TYPES.contains(typeName)) { + assert data instanceof ByteString; + if (typeName == SqlTypeName.VARBINARY && colType.getPrecision() == RelDataType.PRECISION_NOT_SPECIFIED) { + continue; + } + + ByteString byteString = (ByteString) data; + + if (byteString.length() > colPrecision) { + for (int pos = byteString.length(); pos > colPrecision; --pos) { + if (byteString.byteAt(pos - 1) != 0) { + throw new SqlException(STMT_VALIDATION_ERR, "Value too long for type: " + colType); + } + } - assert colPrecision != RelDataType.PRECISION_NOT_SPECIFIED; Review Comment: this is correct assertion. At this point we must not get colType.getPrecision() == RelDataType.PRECISION_NOT_SPECIFIED ########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/datatypes/BinaryInsertCoercionTest.java: ########## @@ -0,0 +1,159 @@ +/* + * 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.sql.engine.planner.datatypes; + +import static org.apache.calcite.sql.type.SqlTypeName.BINARY_TYPES; +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; +import static org.apache.ignite.internal.sql.engine.util.TypeUtils.native2relationalType; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import java.util.stream.Stream; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlKind; +import org.apache.ignite.internal.sql.engine.planner.datatypes.utils.BinaryPair; +import org.apache.ignite.internal.sql.engine.planner.datatypes.utils.TypePair; +import org.apache.ignite.internal.sql.engine.planner.datatypes.utils.Types; +import org.apache.ignite.internal.sql.engine.rel.IgniteKeyValueModify; +import org.apache.ignite.internal.sql.engine.rel.IgniteRel; +import org.apache.ignite.internal.sql.engine.schema.IgniteSchema; +import org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory; +import org.apache.ignite.internal.sql.engine.util.Commons; +import org.apache.ignite.internal.type.NativeType; +import org.apache.ignite.internal.type.VarlenNativeType; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * A set of tests to verify behavior of type coercion for INSERT operations, when values belongs to the BINARY type family. + * + * <p>This tests aim to help to understand in which cases implicit cast will be added to which values. + */ +public class BinaryInsertCoercionTest extends BaseTypeCoercionTest { + @ParameterizedTest + @MethodSource("args") + public void insert( + TypePair pair, + Matcher<RexNode> operandMatcher + ) throws Exception { + IgniteSchema schema = createSchemaWithSingleColumnTable(pair.first()); + + int length = ((VarlenNativeType) pair.second()).length(); + + String byteVal = "x'" + "01".repeat(length) + "'"; Review Comment: why don't you use `BaseTypeCoercionTest#generateLiteral`? -- 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]
