|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.service.arrowflight; |
| 19 | + |
| 20 | +import org.apache.doris.analysis.Expr; |
| 21 | +import org.apache.doris.catalog.ScalarType; |
| 22 | +import org.apache.doris.common.FeConstants; |
| 23 | +import org.apache.doris.nereids.NereidsPlanner; |
| 24 | +import org.apache.doris.nereids.StatementContext; |
| 25 | +import org.apache.doris.nereids.glue.LogicalPlanAdapter; |
| 26 | +import org.apache.doris.nereids.parser.NereidsParser; |
| 27 | +import org.apache.doris.qe.OriginStatement; |
| 28 | +import org.apache.doris.service.arrowflight.sessions.FlightSqlConnectContext; |
| 29 | +import org.apache.doris.thrift.TExprNode; |
| 30 | +import org.apache.doris.thrift.TExprNodeType; |
| 31 | +import org.apache.doris.thrift.TUniqueId; |
| 32 | +import org.apache.doris.utframe.TestWithFeService; |
| 33 | + |
| 34 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 35 | +import org.apache.arrow.vector.types.pojo.Field; |
| 36 | +import org.apache.arrow.vector.types.pojo.FieldType; |
| 37 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 38 | +import org.junit.jupiter.api.Assertions; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +import java.util.ArrayList; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.List; |
| 44 | + |
| 45 | +class FlightSqlExecutionSchemaTest extends TestWithFeService { |
| 46 | + private FlightSqlConnectContext flightContext; |
| 47 | + private boolean previousUnitTest; |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void runBeforeAll() throws Exception { |
| 51 | + previousUnitTest = FeConstants.runningUnitTest; |
| 52 | + FeConstants.runningUnitTest = true; |
| 53 | + createDatabase("execution_schema_test"); |
| 54 | + connectContext.setDatabase("execution_schema_test"); |
| 55 | + createTable("CREATE TABLE schema_input (id INT NOT NULL, encoded STRING NULL) " |
| 56 | + + "DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES ('replication_num' = '1')"); |
| 57 | + flightContext = new FlightSqlConnectContext("schema-peer"); |
| 58 | + flightContext.setEnv(connectContext.getEnv()); |
| 59 | + flightContext.setCurrentUserIdentity(connectContext.getCurrentUserIdentity()); |
| 60 | + flightContext.setSessionVariable(connectContext.getSessionVariable()); |
| 61 | + flightContext.setDatabase("execution_schema_test"); |
| 62 | + flightContext.setQueryId(new TUniqueId(1, 2)); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void runAfterAll() throws Exception { |
| 67 | + FeConstants.runningUnitTest = previousUnitTest; |
| 68 | + flightContext.getFlightSqlChannel().close(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void unaliasedLiteralUsesDifferentWireLabel() throws Exception { |
| 73 | + assertPlannerSchema("SELECT 1", true, false); |
| 74 | + assertPlannerSchema("SELECT 1 AS id", false, false); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void unaliasedFunctionUsesDifferentWireLabel() throws Exception { |
| 79 | + assertPlannerSchema("SELECT unhex(encoded) FROM schema_input WHERE id <= 4 ORDER BY id", true, false); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void constantFoldingNarrowsNullableCast() throws Exception { |
| 84 | + assertPlannerSchema("SELECT CAST(CAST('2026-01-01' AS DATETIME(6)) AS STRING)", true, true); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void nullableColumnRemainsCompatibleWithFilter() throws Exception { |
| 89 | + assertPlannerSchema("SELECT encoded FROM schema_input WHERE encoded IS NOT NULL", false, false); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void tableValuedFunctionUsesQualifiedWireLabel() throws Exception { |
| 94 | + Schema actual = assertPlannerSchema("SELECT * FROM numbers('number' = '1')", true, false); |
| 95 | + Assertions.assertTrue(actual.getFields().get(0).getName().startsWith("_tvf_numbers.")); |
| 96 | + } |
| 97 | + |
| 98 | + private Schema assertPlannerSchema(String sql, boolean differentLabel, boolean narrowedNullability) |
| 99 | + throws Exception { |
| 100 | + flightContext.setThreadLocalInfo(); |
| 101 | + Schema prepared = FlightSqlQuerySchema.analyze(flightContext, sql); |
| 102 | + try (StatementContext statementContext = new StatementContext(flightContext, new OriginStatement(sql, 0))) { |
| 103 | + flightContext.setStatementContext(statementContext); |
| 104 | + LogicalPlanAdapter statement = new LogicalPlanAdapter( |
| 105 | + new NereidsParser().parseSingle(sql), statementContext); |
| 106 | + statementContext.setParsedStatement(statement); |
| 107 | + new NereidsPlanner(statementContext).plan(statement, flightContext.getSessionVariable().toThrift()); |
| 108 | + List<Field> wireFields = new ArrayList<>(); |
| 109 | + for (Expr expression : statement.getResultExprs()) { |
| 110 | + TExprNode node = expression.treeToThrift().getNodes().get(0); |
| 111 | + Assertions.assertEquals(TExprNodeType.SLOT_REF, node.getNodeType()); |
| 112 | + ScalarType type = (ScalarType) expression.getType(); |
| 113 | + ArrowType arrowType = FlightSqlSchemaHelper.getArrowType(type.getPrimitiveType(), |
| 114 | + type.getScalarPrecision(), type.getScalarScale()); |
| 115 | + // BE derives these fields from the serialized output expressions, not analyzed Slot names. |
| 116 | + wireFields.add(new Field(node.getLabel(), |
| 117 | + new FieldType(node.isIsNullable(), arrowType, null), Collections.emptyList())); |
| 118 | + } |
| 119 | + Schema actual = new Schema(wireFields); |
| 120 | + Assertions.assertEquals(differentLabel, |
| 121 | + !prepared.getFields().get(0).getName().equals(actual.getFields().get(0).getName())); |
| 122 | + Assertions.assertEquals(narrowedNullability, |
| 123 | + prepared.getFields().get(0).isNullable() && !actual.getFields().get(0).isNullable()); |
| 124 | + Assertions.assertTrue(FlightSqlQuerySchema.matchesExecutionSchema( |
| 125 | + prepared, actual, statement.getColLabels())); |
| 126 | + return actual; |
| 127 | + } finally { |
| 128 | + flightContext.setStatementContext(null); |
| 129 | + connectContext.setThreadLocalInfo(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void sameArityRenameIsRejectedDespiteIdenticalWireTypes() { |
| 135 | + Schema schema = schema(Field.nullable("old_name", new ArrowType.Utf8())); |
| 136 | + Assertions.assertFalse(FlightSqlQuerySchema.matchesExecutionSchema(schema, schema, |
| 137 | + Collections.singletonList("new_name"))); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void differentTypesAreRejected() { |
| 142 | + Schema prepared = schema(Field.nullable("value", new ArrowType.Int(32, true))); |
| 143 | + Schema actual = schema(Field.nullable("value", new ArrowType.Int(64, true))); |
| 144 | + Assertions.assertFalse(FlightSqlQuerySchema.matchesExecutionSchema(prepared, actual, |
| 145 | + Collections.singletonList("value"))); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void nestedFieldNamesAreNotTransportLabels() { |
| 150 | + Schema prepared = schema(new Field("value", FieldType.nullable(new ArrowType.Struct()), |
| 151 | + Collections.singletonList(Field.nullable("old_name", new ArrowType.Utf8())))); |
| 152 | + Schema actual = schema(new Field("value", FieldType.nullable(new ArrowType.Struct()), |
| 153 | + Collections.singletonList(Field.nullable("new_name", new ArrowType.Utf8())))); |
| 154 | + Assertions.assertFalse(FlightSqlQuerySchema.matchesExecutionSchema(prepared, actual, |
| 155 | + Collections.singletonList("value"))); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void typeMetadataChangesAreRejected() { |
| 160 | + Schema prepared = schema(new Field("value", new FieldType(true, new ArrowType.Utf8(), null, |
| 161 | + Collections.singletonMap("doris_type", "LARGEINT")), Collections.emptyList())); |
| 162 | + Schema actual = schema(Field.nullable("value", new ArrowType.Utf8())); |
| 163 | + Assertions.assertFalse(FlightSqlQuerySchema.matchesExecutionSchema(prepared, actual, |
| 164 | + Collections.singletonList("value"))); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void nullableWideningIsRejected() { |
| 169 | + Schema prepared = schema(Field.notNullable("value", new ArrowType.Utf8())); |
| 170 | + Schema actual = schema(Field.nullable("value", new ArrowType.Utf8())); |
| 171 | + Assertions.assertFalse(FlightSqlQuerySchema.matchesExecutionSchema(prepared, actual, |
| 172 | + Collections.singletonList("value"))); |
| 173 | + } |
| 174 | + |
| 175 | + private static Schema schema(Field field) { |
| 176 | + return new Schema(Collections.singletonList(field)); |
| 177 | + } |
| 178 | +} |
0 commit comments