924060929 commented on code in PR #68853: URL: https://github.com/apache/doris/pull/68853#discussion_r4678040563
########## fe/fe-core/src/test/java/org/apache/doris/nereids/types/FileTypeExpressionTest.java: ########## @@ -0,0 +1,798 @@ +// 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.doris.nereids.types; + +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.Function; +import org.apache.doris.catalog.FunctionSignature; +import org.apache.doris.catalog.FunctionVolatility; +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.catalog.Type; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.exceptions.NotSupportedException; +import org.apache.doris.nereids.rules.analysis.CheckAnalysis; +import org.apache.doris.nereids.rules.analysis.ExpressionAnalyzer; +import org.apache.doris.nereids.rules.expression.check.CheckCast; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.Cast; +import org.apache.doris.nereids.trees.expressions.Divide; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.InPredicate; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator; +import org.apache.doris.nereids.trees.expressions.functions.BoundFunction; +import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature; +import org.apache.doris.nereids.trees.expressions.functions.ImplicitlyCastableSignature; +import org.apache.doris.nereids.trees.expressions.functions.Udf; +import org.apache.doris.nereids.trees.expressions.functions.agg.AnyValue; +import org.apache.doris.nereids.trees.expressions.functions.agg.ArrayAgg; +import org.apache.doris.nereids.trees.expressions.functions.agg.CollectList; +import org.apache.doris.nereids.trees.expressions.functions.agg.CollectSet; +import org.apache.doris.nereids.trees.expressions.functions.agg.Count; +import org.apache.doris.nereids.trees.expressions.functions.agg.CountAtMax; +import org.apache.doris.nereids.trees.expressions.functions.agg.CountAtMin; +import org.apache.doris.nereids.trees.expressions.functions.agg.GroupArrayIntersect; +import org.apache.doris.nereids.trees.expressions.functions.agg.GroupArrayUnion; +import org.apache.doris.nereids.trees.expressions.functions.agg.MapAggV2; +import org.apache.doris.nereids.trees.expressions.functions.agg.Max; +import org.apache.doris.nereids.trees.expressions.functions.agg.MaxBy; +import org.apache.doris.nereids.trees.expressions.functions.agg.MaxMap; +import org.apache.doris.nereids.trees.expressions.functions.agg.Min; +import org.apache.doris.nereids.trees.expressions.functions.agg.MinBy; +import org.apache.doris.nereids.trees.expressions.functions.agg.MinMap; +import org.apache.doris.nereids.trees.expressions.functions.agg.MultiDistinctCount; +import org.apache.doris.nereids.trees.expressions.functions.agg.TopNArray; +import org.apache.doris.nereids.trees.expressions.functions.combinator.StateCombinator; +import org.apache.doris.nereids.trees.expressions.functions.generator.ExplodeFile; +import org.apache.doris.nereids.trees.expressions.functions.generator.ExplodeFileOuter; +import org.apache.doris.nereids.trees.expressions.functions.scalar.ArrayDistinct; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Coalesce; +import org.apache.doris.nereids.trees.expressions.functions.scalar.ElementAt; +import org.apache.doris.nereids.trees.expressions.functions.scalar.FlGetBinary; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Greatest; +import org.apache.doris.nereids.trees.expressions.functions.scalar.If; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Least; +import org.apache.doris.nereids.trees.expressions.functions.scalar.NullIf; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Nvl; +import org.apache.doris.nereids.trees.expressions.functions.udf.AliasUdf; +import org.apache.doris.nereids.trees.expressions.functions.udf.PythonUdaf; +import org.apache.doris.nereids.trees.expressions.functions.udf.PythonUdf; +import org.apache.doris.nereids.trees.expressions.functions.udf.PythonUdtf; +import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral; +import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral; +import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; +import org.apache.doris.nereids.trees.expressions.literal.StringLiteral; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation; +import org.apache.doris.nereids.util.MemoTestUtils; +import org.apache.doris.nereids.util.PlanChecker; +import org.apache.doris.nereids.util.TypeCoercionUtils; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.utframe.TestWithFeService; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Locale; + +public class FileTypeExpressionTest extends TestWithFeService { + private static final String FROM_FILE_SOURCE = " from file_expr_db.file_expr_source"; + + @Override + protected void runBeforeAll() throws Exception { + createDatabase("file_expr_db"); + connectContext.setDatabase("file_expr_db"); + createTable("CREATE TABLE file_expr_source (id INT, path STRING, size BIGINT, " + + "f STRING, files STRING, holder STRING, lookup STRING) " + + "DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES('replication_num'='1')"); + OlapTable source = (OlapTable) Env.getCurrentInternalCatalog().getDbOrAnalysisException("file_expr_db") + .getTableOrAnalysisException("file_expr_source"); + // Install FILE types in the test catalog because CREATE TABLE rejects them. Review Comment: Could we update the PR description and release note to match the split in `adddabfd`? The examples still promise `TO_FILE` and `LIST_FILES`. I checked that both fail to bind on this head. This fixture is a good way to test the planner, but it installs the FILE column directly in the catalog. If this PR ships the type infrastructure first, please say where the first SQL producer and its non-null end-to-end test will land. `branch-4.1` already registered both functions in #69040, so the intended upgrade path matters for users of builds that include that change. ########## fe/fe-core/src/test/java/org/apache/doris/nereids/types/FileTypeExpressionTest.java: ########## @@ -0,0 +1,798 @@ +// 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.doris.nereids.types; + +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.Function; +import org.apache.doris.catalog.FunctionSignature; +import org.apache.doris.catalog.FunctionVolatility; +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.catalog.Type; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.exceptions.NotSupportedException; +import org.apache.doris.nereids.rules.analysis.CheckAnalysis; +import org.apache.doris.nereids.rules.analysis.ExpressionAnalyzer; +import org.apache.doris.nereids.rules.expression.check.CheckCast; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.Cast; +import org.apache.doris.nereids.trees.expressions.Divide; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.InPredicate; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator; +import org.apache.doris.nereids.trees.expressions.functions.BoundFunction; +import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature; +import org.apache.doris.nereids.trees.expressions.functions.ImplicitlyCastableSignature; +import org.apache.doris.nereids.trees.expressions.functions.Udf; +import org.apache.doris.nereids.trees.expressions.functions.agg.AnyValue; +import org.apache.doris.nereids.trees.expressions.functions.agg.ArrayAgg; +import org.apache.doris.nereids.trees.expressions.functions.agg.CollectList; +import org.apache.doris.nereids.trees.expressions.functions.agg.CollectSet; +import org.apache.doris.nereids.trees.expressions.functions.agg.Count; +import org.apache.doris.nereids.trees.expressions.functions.agg.CountAtMax; +import org.apache.doris.nereids.trees.expressions.functions.agg.CountAtMin; +import org.apache.doris.nereids.trees.expressions.functions.agg.GroupArrayIntersect; +import org.apache.doris.nereids.trees.expressions.functions.agg.GroupArrayUnion; +import org.apache.doris.nereids.trees.expressions.functions.agg.MapAggV2; +import org.apache.doris.nereids.trees.expressions.functions.agg.Max; +import org.apache.doris.nereids.trees.expressions.functions.agg.MaxBy; +import org.apache.doris.nereids.trees.expressions.functions.agg.MaxMap; +import org.apache.doris.nereids.trees.expressions.functions.agg.Min; +import org.apache.doris.nereids.trees.expressions.functions.agg.MinBy; +import org.apache.doris.nereids.trees.expressions.functions.agg.MinMap; +import org.apache.doris.nereids.trees.expressions.functions.agg.MultiDistinctCount; +import org.apache.doris.nereids.trees.expressions.functions.agg.TopNArray; +import org.apache.doris.nereids.trees.expressions.functions.combinator.StateCombinator; +import org.apache.doris.nereids.trees.expressions.functions.generator.ExplodeFile; +import org.apache.doris.nereids.trees.expressions.functions.generator.ExplodeFileOuter; +import org.apache.doris.nereids.trees.expressions.functions.scalar.ArrayDistinct; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Coalesce; +import org.apache.doris.nereids.trees.expressions.functions.scalar.ElementAt; +import org.apache.doris.nereids.trees.expressions.functions.scalar.FlGetBinary; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Greatest; +import org.apache.doris.nereids.trees.expressions.functions.scalar.If; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Least; +import org.apache.doris.nereids.trees.expressions.functions.scalar.NullIf; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Nvl; +import org.apache.doris.nereids.trees.expressions.functions.udf.AliasUdf; +import org.apache.doris.nereids.trees.expressions.functions.udf.PythonUdaf; +import org.apache.doris.nereids.trees.expressions.functions.udf.PythonUdf; +import org.apache.doris.nereids.trees.expressions.functions.udf.PythonUdtf; +import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral; +import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral; +import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; +import org.apache.doris.nereids.trees.expressions.literal.StringLiteral; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation; +import org.apache.doris.nereids.util.MemoTestUtils; +import org.apache.doris.nereids.util.PlanChecker; +import org.apache.doris.nereids.util.TypeCoercionUtils; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.utframe.TestWithFeService; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Locale; + +public class FileTypeExpressionTest extends TestWithFeService { + private static final String FROM_FILE_SOURCE = " from file_expr_db.file_expr_source"; + + @Override + protected void runBeforeAll() throws Exception { + createDatabase("file_expr_db"); + connectContext.setDatabase("file_expr_db"); + createTable("CREATE TABLE file_expr_source (id INT, path STRING, size BIGINT, " + + "f STRING, files STRING, holder STRING, lookup STRING) " + + "DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES('replication_num'='1')"); + OlapTable source = (OlapTable) Env.getCurrentInternalCatalog().getDbOrAnalysisException("file_expr_db") + .getTableOrAnalysisException("file_expr_source"); + // Install FILE types in the test catalog because CREATE TABLE rejects them. + source.getColumn("f").setType(Type.FILE); + source.getColumn("files").setType(new org.apache.doris.catalog.ArrayType(Type.FILE)); + source.getColumn("holder").setType(new org.apache.doris.catalog.StructType( + new org.apache.doris.catalog.StructField("f", Type.FILE))); + source.getColumn("lookup").setType(new org.apache.doris.catalog.MapType(Type.STRING, Type.FILE)); + } + + private DataType type(String sql) { + return DataType.convertFromString(sql); + } + + private void assertAnalysisError(String expectedMessage, Executable executable, String... context) { + AnalysisException error = Assertions.assertThrows(AnalysisException.class, executable, + String.join(" ", context)); + Assertions.assertTrue(error.getMessage().contains(expectedMessage), + () -> String.join(" ", context) + ": expected " + expectedMessage + "; actual " + error.getMessage()); + } + + @Test + public void testFileChecksDoNotReconstructPersistedDecimalArithmeticTypes() { + ConnectContext previous = ConnectContext.get(); + try { + ConnectContext context = MemoTestUtils.createConnectContext(); + context.getSessionVariable().enableDecimal256 = false; + Divide divide = new Divide( + new SlotReference("wide", DecimalV3Type.createDecimalV3TypeNotCheck256(52, 20)), + new SlotReference("divisor", DecimalV3Type.createDecimalV3Type(38, 10))); + Alias alias = new Alias(divide, "quotient"); + LogicalOneRowRelation relation = new LogicalOneRowRelation(StatementScopeIdGenerator.newRelationId(), + Collections.singletonList(alias)); + Assertions.assertThrows(NotSupportedException.class, divide::getDataType); + Assertions.assertDoesNotThrow(divide::checkInputDataTypes); + Assertions.assertDoesNotThrow(alias::checkInputDataTypes); + Assertions.assertDoesNotThrow(() -> CheckAnalysis.checkKeyTypes(relation)); + Assertions.assertFalse(context.getSessionVariable().isEnableDecimal256()); + } finally { + if (previous == null) { + ConnectContext.remove(); + } else { + previous.setThreadLocalInfo(); + } + } + } + + @Test + public void testFileChecksDoNotComputeUncoercedNonFileResults() { + for (String function : Arrays.asList("array_max", "array_min")) { + Assertions.assertDoesNotThrow(() -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select " + function + "(null)")); + } + } + + @Test + public void testFunctionsKeepTheirNativeTypeRestrictions() { + for (String expression : Arrays.asList("array_distinct(files)", "array_sort(files)")) { + String expectedMessage; + if (expression.startsWith("array_distinct(")) { + expectedMessage = "array_distinct does not support element type"; + } else { + expectedMessage = "array_sort does not support types: ARRAY<FILE>"; + } + assertAnalysisError(expectedMessage, + () -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select " + expression + FROM_FILE_SOURCE), expression); + } + for (String expression : Arrays.asList("first_value(f) over(order by id)", "last_value(f) over(order by id)", + "nth_value(f, 1) over(order by id)", "array_sortby(files, array(id))")) { + Assertions.assertDoesNotThrow(() -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select " + expression + FROM_FILE_SOURCE), expression); + } + } + + @Test + public void testHigherOrderFunctionsUseNativeSignatures() { + for (String expression : Arrays.asList("array_map(x -> x, files)", + "array_map(x -> element_at(x,'size'), files)", + "array_filter(x -> element_at(x,'size') > 0, files)", + "array_map(x -> f, array(id))", "array_map((x,y) -> x, array(id), files)")) { + Assertions.assertDoesNotThrow(() -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select " + expression + FROM_FILE_SOURCE), expression); + } + for (String expression : Arrays.asList("array_map(x -> x + 1, array(id))", + "array_map(x -> element_at(f,'size') + x, array(id))")) { + Assertions.assertDoesNotThrow(() -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select " + expression + FROM_FILE_SOURCE), expression); + } + } + + @Test + public void testAliasFunctionsUseDeclaredSignatures() { + for (String sqlType : Arrays.asList("FILE", "ARRAY<FILE>", "STRUCT<f:FILE>", "MAP<STRING,FILE>")) { + AliasUdf alias = new AliasUdf("file_alias", Collections.singletonList(type(sqlType)), + new NullLiteral(), Collections.singletonList("f"), Collections.emptyMap(), new NullLiteral()); + Assertions.assertDoesNotThrow(() -> TypeCoercionUtils.processBoundFunction(alias), sqlType); + } + AliasUdf ordinary = new AliasUdf("ordinary_alias", Collections.singletonList(BigIntType.INSTANCE), + new NullLiteral(), Collections.singletonList("n"), Collections.emptyMap(), new NullLiteral()); + Assertions.assertDoesNotThrow(() -> TypeCoercionUtils.processBoundFunction(ordinary)); + } + + @Test + public void testPublicFieldSelectors() { + Expression file = new SlotReference("f", FileType.INSTANCE); + Assertions.assertEquals(BigIntType.INSTANCE, + TypeCoercionUtils.processBoundFunction(new ElementAt(file, new StringLiteral("SIZE"))).getDataType()); + Assertions.assertTrue(new ElementAt(file, new StringLiteral("uri")).nullable()); + for (String selector : Arrays.asList("inline", "INLINE")) { + assertAnalysisError("use FL_GET_BINARY", + () -> TypeCoercionUtils.processBoundFunction(new ElementAt(file, new StringLiteral(selector)))); + } + for (Expression selector : Arrays.asList(new StringLiteral("path"), + new IntegerLiteral(1), new SlotReference("s", StringType.INSTANCE))) { + assertAnalysisError(selector instanceof StringLiteral + ? "the specified field name path was not found" + : "element_at over FILE requires a constant public field name", + () -> TypeCoercionUtils.processBoundFunction(new ElementAt(file, selector))); + } + } + + @Test + public void testFileJsonCastsAreUnsupported() { + for (boolean strict : Arrays.asList(true, false)) { + for (String wrapper : Arrays.asList("%s", "ARRAY<%s>", "STRUCT<f:%s>", "MAP<STRING,%s>")) { + DataType file = type(String.format(wrapper, "FILE")); + DataType json = type(String.format(wrapper, "JSON")); + Assertions.assertFalse(CheckCast.check(file, json, strict)); + Assertions.assertFalse(CheckCast.check(json, file, strict)); + Assertions.assertFalse(CheckCast.check(file, JsonType.INSTANCE, strict)); + Assertions.assertFalse(CheckCast.check(JsonType.INSTANCE, file, strict)); + } + } + } + + @Test + public void testToJsonAcceptsFile() { + for (String value : Arrays.asList("f", "files", "holder", "lookup", + "array(f)", "named_struct('f', f)", "map('f', f)")) { + Plan plan = PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select to_json(" + value + ")" + FROM_FILE_SOURCE).getPlan(); + Assertions.assertEquals(JsonType.INSTANCE, plan.getOutput().get(0).getDataType()); + } + } + + @Test + public void testFileCastUsesOrdinaryTypeChecksAndNullLiteralCoercion() { + Assertions.assertFalse(Cast.castNullable(false, FileType.INSTANCE.publicStructType(), FileType.INSTANCE)); + Assertions.assertTrue(Cast.castNullable(true, FileType.INSTANCE.publicStructType(), FileType.INSTANCE)); + Assertions.assertTrue(Cast.castNullable(false, JsonType.INSTANCE, FileType.INSTANCE)); + Assertions.assertFalse(CheckCast.check(JsonType.INSTANCE, FileType.INSTANCE, true)); + Expression converted = TypeCoercionUtils.castIfNotSameType(new NullLiteral(FileType.INSTANCE), + JsonType.INSTANCE); + Assertions.assertTrue(converted instanceof NullLiteral); + Assertions.assertEquals(JsonType.INSTANCE, converted.getDataType()); + assertAnalysisError("can not cast", () -> TypeCoercionUtils.castIfNotSameType( + new SlotReference("s", FileType.INSTANCE.publicStructType()), FileType.INSTANCE)); + Assertions.assertFalse(CheckCast.check(IntegerType.INSTANCE, FileType.INSTANCE, false)); + } + + @Test + public void testFileCastsAreUnsupported() { + String metadata = "uri:STRING,offset:BIGINT,size:BIGINT,content_type:STRING,checksum:STRING"; + String fields = metadata + ",inline:VARBINARY"; + for (boolean strict : Arrays.asList(true, false)) { + for (String source : Arrays.asList("STRUCT<" + fields + ">", + "STRUCT<inline:VARBINARY,checksum:VARCHAR(10),size:BIGINT,uri:CHAR(10)," + + "content_type:STRING,offset:BIGINT>")) { + DataType value = type(source); + Assertions.assertFalse(CheckCast.check(value, FileType.INSTANCE, strict), source); + Assertions.assertFalse(CheckCast.check(FileType.INSTANCE, value, strict), source); + } + for (String source : Arrays.asList("STRING", "JSON", "VARIANT", "INT", "STRUCT<uri:STRING>", + "STRUCT<" + metadata + ">", + "STRUCT<" + metadata + ",inline:STRING>", + "STRUCT<" + fields + ",extra:STRING>", + "STRUCT<" + metadata + ",payload:VARBINARY>", + "STRUCT<uri:STRING,offset:INT,size:BIGINT,content_type:STRING,checksum:STRING,inline:VARBINARY>")) { + Assertions.assertFalse(CheckCast.check(type(source), FileType.INSTANCE, strict), source); + Assertions.assertFalse(CheckCast.check(FileType.INSTANCE, type(source), strict), source); + } + Assertions.assertFalse(CheckCast.check(type("ARRAY<STRUCT<" + fields + ">>"), + type("ARRAY<FILE>"), strict)); + for (String wrapper : Arrays.asList("%s", "ARRAY<%s>", "MAP<STRING,%s>", "STRUCT<f:%s>", + "ARRAY<STRUCT<f:%s>>")) { + DataType source = type(String.format(wrapper, "STRUCT<" + fields + ">")); + DataType target = type(String.format(wrapper, "FILE")); + Assertions.assertFalse(CheckCast.check(source, target, strict), wrapper); + Assertions.assertFalse(CheckCast.check(target, source, strict), wrapper); + } + Assertions.assertTrue(CheckCast.check(StringType.INSTANCE, type("ARRAY<STRUCT<" + fields + ">>"), strict)); + Assertions.assertFalse(CheckCast.check(StringType.INSTANCE, type("ARRAY<FILE>"), strict)); + } + StructType nullableInline = new StructType(Arrays.asList( + new StructField("uri", StringType.INSTANCE, false, ""), + new StructField("offset", BigIntType.INSTANCE, true, ""), + new StructField("size", BigIntType.INSTANCE, true, ""), + new StructField("content_type", StringType.INSTANCE, true, ""), + new StructField("checksum", StringType.INSTANCE, true, ""), + new StructField("inline", NullType.INSTANCE, true, ""))); + Assertions.assertFalse(CheckCast.check(nullableInline, FileType.INSTANCE, true)); + Assertions.assertFalse(CheckCast.check(nullableInline, FileType.INSTANCE, false)); + Assertions.assertFalse(CheckCast.check(FileType.INSTANCE, nullableInline, true)); + StructType requiredInline = new StructType(FileType.INSTANCE.getFields().stream() + .map(field -> field.getName().equals("inline") + ? new StructField("inline", VarBinaryType.INSTANCE, false, "") : field) + .collect(java.util.stream.Collectors.toList())); + Assertions.assertFalse(CheckCast.check(requiredInline, FileType.INSTANCE, true)); + Assertions.assertFalse(CheckCast.check(FileType.INSTANCE, requiredInline, true)); + Assertions.assertFalse(CheckCast.check(FileType.INSTANCE, requiredInline, false)); + Assertions.assertTrue(TypeCoercionUtils.implicitCast(NullType.INSTANCE, FileType.INSTANCE).isPresent()); + Assertions.assertTrue(TypeCoercionUtils.implicitCast(FileType.INSTANCE, FileType.INSTANCE).isPresent()); + } + + @Test + public void testFileCastsRejectNullIdentityAndWholeContainers() { + for (boolean strict : Arrays.asList(true, false)) { + for (String container : Arrays.asList("FILE", "ARRAY<FILE>", "MAP<STRING,FILE>", "STRUCT<f:FILE>", + "ARRAY<STRUCT<f:FILE>>", "STRUCT<f:ARRAY<FILE>>", "MAP<STRING,ARRAY<FILE>>")) { + DataType nested = type(container); + for (String source : Arrays.asList("STRING", "VARCHAR(20)", "CHAR(20)", "VARIANT")) { + Assertions.assertFalse(CheckCast.check(type(source), nested, strict), source + " -> " + container); + } + Assertions.assertFalse(CheckCast.check(nested, VariantType.INSTANCE, strict), + container + " -> VARIANT"); + Assertions.assertFalse(CheckCast.check(nested, StringType.INSTANCE, strict), + container + " -> STRING"); + Assertions.assertFalse(CheckCast.check(NullType.INSTANCE, nested, strict), "NULL -> " + container); + Assertions.assertFalse(CheckCast.check(nested, nested, strict), container); + } + for (String wrapper : Arrays.asList("ARRAY<%s>", "MAP<STRING,%s>", "STRUCT<f:%s>")) { + for (String leaf : Arrays.asList("STRING", "VARIANT")) { + Assertions.assertFalse(CheckCast.check(type(String.format(wrapper, leaf)), + type(String.format(wrapper, "ARRAY<FILE>")), strict), wrapper + leaf); + } + Assertions.assertFalse(CheckCast.check(type(String.format(wrapper, "VARIANT")), + type(String.format(wrapper, "FILE")), strict), wrapper); + Assertions.assertFalse(CheckCast.check(type(String.format(wrapper, "FILE")), + type(String.format(wrapper, "VARIANT")), strict), wrapper); + } + Assertions.assertTrue(CheckCast.check(VariantType.INSTANCE, type("ARRAY<INT>"), strict)); + } + } + + @Test + public void testSqlRejectsNullAndIdentityFileCasts() { + for (String cast : Arrays.asList("cast", "try_cast")) { + for (String[] value : Arrays.asList( + new String[] {"FILE", "f"}, + new String[] {"ARRAY<FILE>", "files"}, + new String[] {"STRUCT<f:FILE>", "holder"}, + new String[] {"MAP<STRING,FILE>", "lookup"})) { + for (String input : Arrays.asList("null", value[1])) { + String sql = "select " + cast + "(" + input + " as " + value[0] + ")" + FROM_FILE_SOURCE; + assertAnalysisError("can not cast", () -> PlanChecker.from(connectContext).analyze(sql), sql); + } + } + } + } + + @Test + public void testFileContainersRejectChangesToOtherFields() { + for (boolean strict : Arrays.asList(true, false)) { + for (String wrapper : Arrays.asList("%s", "ARRAY<%s>", "MAP<STRING,%s>")) { + DataType source = type(String.format(wrapper, "STRUCT<f:FILE,n:INT>")); + DataType target = type(String.format(wrapper, "STRUCT<f:FILE,n:BIGINT>")); + Assertions.assertFalse(CheckCast.check(source, target, strict), + source.toSql() + " -> " + target.toSql()); + } + } + } + + @Test + public void testConditionalFunctionsAcceptFileAndRejectMixedTypes() { + Expression file = new SlotReference("f", FileType.INSTANCE); + Expression structured = new SlotReference("s", type("STRUCT<uri:STRING>")); + assertAnalysisError("Can not find the compatibility function signature:", + () -> TypeCoercionUtils.processBoundFunction(new If(BooleanLiteral.TRUE, file, structured))); + assertAnalysisError("Can not find the compatibility function signature:", + () -> TypeCoercionUtils.processBoundFunction(new Coalesce(structured, file))); + for (DataType valueType : Arrays.asList(FileType.INSTANCE, type("ARRAY<FILE>"), + type("STRUCT<f:FILE>"), type("MAP<STRING,FILE>"))) { + Expression value = new SlotReference("v", valueType); + for (BoundFunction function : Arrays.asList(new Coalesce(new NullLiteral(), value), + new Coalesce(value, new NullLiteral(valueType)), + new Coalesce(new NullLiteral(valueType), new NullLiteral()), + new If(BooleanLiteral.TRUE, value, value), + new If(BooleanLiteral.TRUE, new NullLiteral(), value), + new If(BooleanLiteral.FALSE, new NullLiteral(valueType), new NullLiteral()), + new Nvl(value, new NullLiteral()), new Nvl(new NullLiteral(), value))) { + Assertions.assertEquals(valueType, + TypeCoercionUtils.processBoundFunction(function).getDataType(), function.toSql()); + } + } + } + + @Test + public void testSqlFileValuePropagationAndContainerConstruction() { + for (String expression : Arrays.asList("array(f, null)", "struct(f)", "named_struct('asset', f)", + "map('asset', f)", "array(holder, null)", "map('asset', files)", + "reverse(files)", "array_slice(files, 1, 1)", "array_concat(files, files)", + "map_values(lookup)", "map_keys(lookup)", "array_size(files)", "map_size(lookup)", + "cardinality(files)", "cardinality(lookup)", + "case when id > 0 then f else null end")) { + Assertions.assertDoesNotThrow(() -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select " + expression + FROM_FILE_SOURCE), expression); + } + for (String expression : Arrays.asList("map('asset', f) = map('asset', f)", + "array_distinct(array(f))")) { + String expectedMessage; + if (expression.contains(" = ")) { + expectedMessage = "comparison predicate could not contains complex type"; + } else { + expectedMessage = "array_distinct does not support element type"; + } + assertAnalysisError(expectedMessage, + () -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select " + expression + FROM_FILE_SOURCE), expression); + } + for (String expression : Arrays.asList("coalesce(f, f)", "if(true, f, null)", "ifnull(null, f)", + "fl_get_binary(element_at(array(f, null), 1))", + "fl_get_binary(element_at(named_struct('asset', f), 'asset'))", + "fl_get_binary(element_at(map('asset', f), 'asset'))")) { + Assertions.assertDoesNotThrow(() -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select " + expression + FROM_FILE_SOURCE), expression); + } + } + + @Test + public void testRecursiveComparisonGates() { + for (String sql : Arrays.asList("FILE", "ARRAY<FILE>", "STRUCT<f:FILE>", "MAP<STRING,FILE>")) { + Expression value = new SlotReference("v", type(sql)); + assertAnalysisError(value.getDataType().isArrayType() + ? "data type " + sql + " could not used in ComparisonPredicate" + : "comparison predicate could not contains complex type", + () -> TypeCoercionUtils.processComparisonPredicate(new EqualTo(value, value))); + assertAnalysisError("in predicate could not contains complex type", + () -> TypeCoercionUtils.processInPredicate( + new InPredicate(value, Collections.singletonList(value)))); + assertAnalysisError("does not support type", + () -> value.getDataType().checkKeyUse(DataType.KeyUse.ORDERING, "ORDER BY")); + assertAnalysisError("does not support type", + () -> value.getDataType().checkKeyUse(DataType.KeyUse.GROUP_EQUALITY, "GROUP BY")); + } + } + + @Test + public void testInPredicateRejectsOrdinaryStruct() { + for (String sqlType : Arrays.asList("STRUCT<n:BIGINT>", "STRUCT<m:MAP<STRING,INT>>", + "STRUCT<j:JSON>", "STRUCT<a:ARRAY<JSON>>")) { + Expression value = new SlotReference("s", type(sqlType)); + InPredicate predicate = new InPredicate(value, Collections.singletonList(value)); + assertAnalysisError("in predicate could not contains complex type", + () -> TypeCoercionUtils.processInPredicate(predicate), sqlType); + } + } + + @Test + public void testInPredicateRejectsFileInAnyArgument() { + for (String sqlType : Arrays.asList("FILE", "ARRAY<FILE>", "STRUCT<f:FILE>", + "STRUCT<a:ARRAY<FILE>>", "MAP<STRING,FILE>")) { + Expression value = new SlotReference("f", type(sqlType)); + for (InPredicate predicate : Arrays.asList(new InPredicate(value, Collections.singletonList(value)), + new InPredicate(new NullLiteral(), Collections.singletonList(value)))) { + assertAnalysisError("in predicate could not contains complex type", + predicate::checkLegalityBeforeTypeCoercion, sqlType); + assertAnalysisError("in predicate could not contains complex type", + () -> TypeCoercionUtils.processInPredicate(predicate), sqlType); + } + } + } + + @Test + public void testElementAtAndExplodeFields() { + Expression file = new SlotReference("f", FileType.INSTANCE); + for (StructField field : FileType.INSTANCE.getFields()) { + if (field.getName().equals("inline")) { + continue; + } + Expression accessed = ExpressionAnalyzer.analyzeFunction(null, null, + new ElementAt(file, new StringLiteral(field.getName().toUpperCase(Locale.ROOT)))); + Assertions.assertTrue(accessed instanceof ElementAt); + Assertions.assertEquals(field.getName().toUpperCase(Locale.ROOT), + ((StringLiteral) accessed.child(1)).getStringValue()); + Assertions.assertEquals(field.getDataType(), accessed.getDataType()); + Assertions.assertTrue(accessed.nullable()); + } + FlGetBinary binary = new FlGetBinary(new SlotReference("required", FileType.INSTANCE, false)); + Assertions.assertEquals(VarBinaryType.INSTANCE, binary.getDataType()); + Assertions.assertTrue(binary.nullable()); + for (BoundFunction explode : Arrays.asList(new ExplodeFile(file), new ExplodeFileOuter(file))) { + Assertions.assertEquals(FileType.INSTANCE.publicStructType(), + TypeCoercionUtils.processBoundFunction(explode).getDataType()); + } + SlotReference struct = new SlotReference("s", FileType.INSTANCE.publicStructType()); + Expression accessed = TypeCoercionUtils.processBoundFunction( + new ElementAt(struct, new StringLiteral("INLINE"))); + Assertions.assertTrue(accessed instanceof ElementAt); + Assertions.assertSame(struct, accessed.child(0)); + Assertions.assertEquals(VarBinaryType.INSTANCE, accessed.getDataType()); + Assertions.assertEquals("INLINE", ((StringLiteral) accessed.child(1)).getStringValue()); + } + + @Test + public void testFunctionParameterAndAssignmentBoundaries() { + DataType struct = type("STRUCT<uri:STRING>"); + for (DataType source : Arrays.asList(struct, JsonType.INSTANCE)) { + Assertions.assertFalse(ImplicitlyCastableSignature.isImplicitlyCastable(FileType.INSTANCE, source)); + Assertions.assertFalse(ExplicitlyCastableSignature.isExplicitlyCastable(FileType.INSTANCE, source)); + } + Assertions.assertEquals(ExplicitlyCastableSignature.isExplicitlyCastable( + StringType.INSTANCE, FileType.INSTANCE.publicStructType()), + ExplicitlyCastableSignature.isExplicitlyCastable(StringType.INSTANCE, FileType.INSTANCE)); + Assertions.assertFalse(ImplicitlyCastableSignature.isImplicitlyCastable( + FileType.INSTANCE, StringType.INSTANCE)); + Assertions.assertTrue(ImplicitlyCastableSignature.isImplicitlyCastable( + type("STRUCT<f:STRING>"), type("STRUCT<f:FILE>"))); + Assertions.assertTrue(ExplicitlyCastableSignature.isExplicitlyCastable( + type("STRUCT<f:STRING>"), type("STRUCT<f:FILE>"))); + Assertions.assertTrue(ImplicitlyCastableSignature.isImplicitlyCastable( + type("STRUCT<f:FILE>"), type("STRUCT<f:STRUCT<uri:STRING>>"))); + assertAnalysisError("can not cast from origin type FILE to target type=TEXT", + () -> TypeCoercionUtils.castIfNotSameType( + new SlotReference("f", FileType.INSTANCE), StringType.INSTANCE)); + } + + @Test + public void testGenericAggregatesUseNativeSignatures() { + Expression key = new IntegerLiteral(1); + for (String sqlType : Arrays.asList("FILE", "ARRAY<FILE>", "STRUCT<f:FILE>", "MAP<STRING,FILE>")) { + Expression file = new SlotReference("f", type(sqlType)); + for (BoundFunction function : Arrays.asList(new Count(file), new AnyValue(file), new ArrayAgg(file), + new CollectList(file), new MinBy(file, key), new MaxBy(file, key), new MapAggV2(key, file))) { + Assertions.assertDoesNotThrow(() -> TypeCoercionUtils.processBoundFunction(function), + function.getName() + " " + sqlType); + } + assertAnalysisError("COUNT DISTINCT could not process type", + () -> new Count(true, file).checkLegalityAfterRewrite(), sqlType); + assertAnalysisError("array_distinct does not support element type", + () -> TypeCoercionUtils.processBoundFunction( + new ArrayDistinct(new SlotReference("a", ArrayType.of(file.getDataType())))), sqlType); + Expression mapWithFileValue = new SlotReference("m", MapType.of(StringType.INSTANCE, file.getDataType())); + Expression mapWithFileKey = new SlotReference("mk", MapType.of(file.getDataType(), IntegerType.INSTANCE)); + for (BoundFunction rejected : Arrays.asList(new Min(file), new Max(file), + new CountAtMin(file), new CountAtMax(file), + new MinBy(key, file), new MaxBy(key, file), + new MinMap(mapWithFileValue), new MaxMap(mapWithFileValue))) { + String expectedMessage = sqlType.equals("ARRAY<FILE>") + ? "Can not find the compatibility function signature" + : "column must use with specific function"; + assertAnalysisError(expectedMessage, + () -> TypeCoercionUtils.processBoundFunction(rejected), + rejected.getName() + " " + sqlType); + } + for (BoundFunction rejectedBySignature : Arrays.asList( + new CollectSet(file), new MapAggV2(file, key), + new TopNArray(file, key), + new MultiDistinctCount(file), + new MinMap(mapWithFileKey), new MaxMap(mapWithFileKey), + new GroupArrayIntersect(new SlotReference("a", ArrayType.of(file.getDataType()))), + new GroupArrayUnion(new SlotReference("a", ArrayType.of(file.getDataType()))), + new Greatest(file, file), new Least(file, file), new NullIf(file, file), + StateCombinator.create(new CollectSet(file)))) { + assertAnalysisError("Can not find the compatibility function signature", + () -> TypeCoercionUtils.processBoundFunction(rejectedBySignature), + rejectedBySignature.getName() + " " + sqlType); + } + } + Expression arrayOfFile = new SlotReference("af", type("ARRAY<FILE>")); + assertAnalysisError("Can not find the compatibility function signature", + () -> TypeCoercionUtils.processBoundFunction(StateCombinator.create(new Min(arrayOfFile)))); + } + + @Test + public void testGenericAggregateSqlAdmission() { + for (String value : Arrays.asList("f", "files", "holder", "lookup")) { + String input = " from (select " + value + " as f" + FROM_FILE_SOURCE + ") t"; + for (String aggregate : Arrays.asList("count(f)", "count(*)", "any_value(f)", "array_agg(f)", + "collect_list(f)", "group_array(f)", "min_by(f, 1)", "max_by(f, 1)", "map_agg(1, f)")) { + Assertions.assertDoesNotThrow(() -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select " + aggregate + input), aggregate + " " + value); + } + } + } + + @Test + public void testPythonFunctionsUseDeclaredSignaturesAtBinding() { + for (DataType fileType : Arrays.asList(FileType.INSTANCE, type("ARRAY<FILE>"), + type("STRUCT<f:FILE>"), type("MAP<STRING,FILE>"))) { + for (String kind : Arrays.asList("scalar", "aggregate", "table")) { + Assertions.assertDoesNotThrow(() -> TypeCoercionUtils.processBoundFunction( + pythonFunction(kind, fileType, IntegerType.INSTANCE, IntegerType.INSTANCE))); + Assertions.assertDoesNotThrow(() -> TypeCoercionUtils.processBoundFunction( + (BoundFunction) pythonFunction(kind, fileType, IntegerType.INSTANCE, IntegerType.INSTANCE) + .withChildren(Collections.singletonList(new NullLiteral())))); + Assertions.assertDoesNotThrow(() -> TypeCoercionUtils.processBoundFunction( + pythonFunction(kind, IntegerType.INSTANCE, fileType, IntegerType.INSTANCE))); + } + Assertions.assertDoesNotThrow(() -> TypeCoercionUtils.processBoundFunction( + pythonFunction("aggregate", IntegerType.INSTANCE, IntegerType.INSTANCE, fileType))); + } + } + + @Test + public void testPythonFunctionsStillAcceptOrdinaryTypesAtBinding() { + for (DataType type : Arrays.asList(IntegerType.INSTANCE, StringType.INSTANCE, type("ARRAY<INT>"), + type("STRUCT<v:INT>"), type("MAP<STRING,INT>"))) { + for (String kind : Arrays.asList("scalar", "aggregate", "table")) { + Assertions.assertDoesNotThrow(() -> TypeCoercionUtils.processBoundFunction( + pythonFunction(kind, type, type, type))); + } + Assertions.assertDoesNotThrow(() -> TypeCoercionUtils.processBoundFunction( + pythonFunction("aggregate", type, type, null))); + } + } + + private BoundFunction pythonFunction(String kind, DataType argumentType, DataType returnType, + DataType intermediateType) { + Expression argument = new SlotReference("v", argumentType); + FunctionSignature signature = FunctionSignature.ret( + kind.equals("table") ? ArrayType.of(returnType) : returnType).args(argumentType); + if (kind.equals("aggregate")) { + return new PythonUdaf("py_agg", 1, "db1", Function.BinaryType.PYTHON_UDF, + signature, intermediateType, Function.NullableMode.ALWAYS_NULLABLE, + FunctionVolatility.IMMUTABLE, Udf.createVolatileIdentity(FunctionVolatility.IMMUTABLE), + null, "Agg", null, null, null, null, null, null, null, false, "", false, 360, + "3.10.2", "", argument); + } + if (kind.equals("table")) { + return new PythonUdtf("py_table", 1, "db1", Function.BinaryType.PYTHON_UDF, + signature, Function.NullableMode.ALWAYS_NULLABLE, FunctionVolatility.IMMUTABLE, + Udf.createVolatileIdentity(FunctionVolatility.IMMUTABLE), + null, "evaluate", null, null, "", false, 360, "3.10.2", "", argument); + } + return new PythonUdf("py_scalar", 1, "db1", Function.BinaryType.PYTHON_UDF, + signature, Function.NullableMode.ALWAYS_NULLABLE, FunctionVolatility.IMMUTABLE, + Udf.createVolatileIdentity(FunctionVolatility.IMMUTABLE), + null, "evaluate", null, null, "", false, 360, "3.10.2", "", argument); + } + + @Test + public void testSqlValueTransportAndExplodeAliases() { + for (String sql : Arrays.asList( + "select element_at(f, 'uri')" + FROM_FILE_SOURCE, + "select fl_get_binary(f)" + FROM_FILE_SOURCE, + "select fl_get_binary(null)", + "select element_at(f, 'SIZE')" + FROM_FILE_SOURCE, + "select array(f)" + FROM_FILE_SOURCE + " union all select array(f)" + FROM_FILE_SOURCE, + "select count(f), count(*)" + FROM_FILE_SOURCE, + "select any_value(distinct f)" + FROM_FILE_SOURCE, + "select element_at(f, 'uri'), count(*)" + FROM_FILE_SOURCE + " group by element_at(f, 'uri')", + "select distinct element_at(f, 'uri')" + FROM_FILE_SOURCE, + "select u, o, s, c, h, b" + FROM_FILE_SOURCE + " lateral view explode_file(f) e as u, o, s, c, h, b", + "select u, o, s, c, h, b" + FROM_FILE_SOURCE + + " lateral view explode_file_outer(f) e as u, o, s, c, h, b")) { + Assertions.assertDoesNotThrow( + () -> PlanChecker.from(MemoTestUtils.createConnectContext()).analyze(sql), sql); + } + } + + @Test + public void testInlineAndExplodeSqlOutputSchema() { + Plan getters = PlanChecker.from(MemoTestUtils.createConnectContext()).analyze( + "select fl_get_binary(f), FL_GET_BINARY(f)" + FROM_FILE_SOURCE).getPlan(); + Assertions.assertEquals(2, getters.getOutput().size()); + getters.getOutput().forEach(output -> { + Assertions.assertEquals(VarBinaryType.INSTANCE, output.getDataType()); + Assertions.assertTrue(output.nullable()); + }); + for (String function : Arrays.asList("explode_file", "explode_file_outer")) { + Plan expanded = PlanChecker.from(MemoTestUtils.createConnectContext()).analyze( + "select u, o, s, c, h, b" + FROM_FILE_SOURCE + + " lateral view " + function + "(f) e as u, o, s, c, h, b").getPlan(); + Assertions.assertEquals(6, expanded.getOutput().size()); + Assertions.assertEquals(VarBinaryType.INSTANCE, expanded.getOutput().get(5).getDataType()); + Assertions.assertTrue(expanded.getOutput().stream().allMatch(Expression::nullable)); + } + } + + @Test + public void testUnionRejectsFileAndStruct() { + assertAnalysisError("Can not find compatible type", () -> PlanChecker.from(MemoTestUtils.createConnectContext()) + .analyze("select f" + FROM_FILE_SOURCE + " union all select named_struct('uri', 'urn:x', 'offset', " + + "cast(0 as bigint), 'size', cast(0 as bigint), 'content_type', '', 'checksum', '', " + + "'inline', null)")); + } + + @Test + public void testSqlOperatorRejections() { + for (String sql : Arrays.asList( + "select f = f" + FROM_FILE_SOURCE, + "select named_struct('f', f) in (named_struct('f', f))" + FROM_FILE_SOURCE, + "select f" + FROM_FILE_SOURCE + " order by f", + "select f" + FROM_FILE_SOURCE + " group by f", + "select distinct f" + FROM_FILE_SOURCE, + "select distinct named_struct('f', f)" + FROM_FILE_SOURCE, + "select f" + FROM_FILE_SOURCE + " union select f" + FROM_FILE_SOURCE, + "select f" + FROM_FILE_SOURCE + " intersect select f" + FROM_FILE_SOURCE, + "select min(f)" + FROM_FILE_SOURCE, + "select element_at(f, 'missing')" + FROM_FILE_SOURCE, + "select u" + FROM_FILE_SOURCE + " lateral view explode_file(f) e as u, o, s, c, h", + "select u" + FROM_FILE_SOURCE + " lateral view explode_file_outer(f) e as u, o, s, c, h")) { + String expectedMessage = "does not support type"; + if (sql.contains("lateral view")) { + expectedMessage = "has 6 columns available but 5 columns specified"; + } else if (sql.contains("missing")) { + expectedMessage = "the specified field name missing was not found"; + } else if (sql.contains(" = ")) { + expectedMessage = "comparison predicate could not contains complex type"; + } else if (sql.contains(" in (")) { + expectedMessage = "in predicate could not contains complex type"; + } else if (sql.startsWith("select min(")) { + expectedMessage = "column must use with specific function"; + } + assertAnalysisError(expectedMessage, + () -> PlanChecker.from(MemoTestUtils.createConnectContext()).analyze(sql), sql); + } + } + + @Test + public void testAggregateOrderByAndKeyRestrictionsSql() { + Assertions.assertDoesNotThrow(() -> PlanChecker.from(MemoTestUtils.createConnectContext()).analyze( + "select group_concat(path order by size)" + FROM_FILE_SOURCE)); + for (String sql : Arrays.asList( + "select group_concat(path order by f)" + FROM_FILE_SOURCE, + "select group_concat(path order by array(f))" + FROM_FILE_SOURCE)) { + assertAnalysisError("ORDER BY does not support type", + () -> PlanChecker.from(MemoTestUtils.createConnectContext()).analyze(sql), sql); + } + for (String select : Arrays.asList( + "select collect_list(distinct f)", + "select array_agg(distinct f)")) { + assertAnalysisError("does not support type", + () -> PlanChecker.from(MemoTestUtils.createConnectContext()).analyze(select + FROM_FILE_SOURCE), + select); + } + for (String select : Arrays.asList( + "select min(array(f))", Review Comment: [P2] Please add `array_sorted_merge` to the ordering checks. With this fixture I ran `array_sorted_merge((x, y) -> if(element_at(x, 'uri') = element_at(y, 'uri'), x, null), array(l.f), array(r.f), true)` through FE analysis and rewrite. Both succeeded. For two non-null files with different URIs, the lambda returns NULL and the BE merge calls `compare_at` to choose which element to emit first. `ColumnFile::compare_at` throws. `ArraySortedMerge#checkLegalityBeforeTypeCoercion()` already resolves both element types and checks that they match. Checking `KeyUse.ORDERING` on that element type there would also cover nested FILE values. The same query with integer elements makes a positive control. -- 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]
