kgyrtkirk commented on code in PR #17699: URL: https://github.com/apache/druid/pull/17699#discussion_r1946530944
########## sql/src/test/java/org/apache/druid/sql/calcite/util/FakeIndexTaskUtil.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.druid.sql.calcite.util; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.jsontype.NamedType; +import org.apache.druid.data.input.InputFormat; +import org.apache.druid.data.input.InputSource; +import org.apache.druid.data.input.impl.LocalInputSource; +import org.apache.druid.quidem.ProjectPathUtils; +import org.apache.druid.segment.indexing.DataSchema; +import org.apache.druid.segment.indexing.IOConfig; +import org.apache.druid.segment.indexing.IngestionSpec; +import org.apache.druid.segment.indexing.TuningConfig; +import org.apache.druid.sql.calcite.util.datasets.InputSourceBasedTestDataset; +import org.apache.druid.sql.calcite.util.datasets.TestDataSet; + +import java.io.File; +import java.io.IOException; + +/** + * Utility class to create {@link TestDataSet} from fake indexing tasks. + * + * Goal is to let the users utilize the ingestion api to create test data. + */ +public class FakeIndexTaskUtil +{ + public static TestDataSet makeDS(ObjectMapper objectMapper, File src) + { + try { + ObjectMapper om = objectMapper.copy(); + om.registerSubtypes(new NamedType(MyIOConfigType.class, "index_parallel")); + FakeIndexTask indexTask = om.readValue(src, FakeIndexTask.class); + FakeIngestionSpec spec = indexTask.spec; Review Comment: I went thru a few of those approaches <details> <summary>java code</summary> ``` public static TestDataSet makeDS(ObjectMapper objectMapper, File src) { try { Map<String, Object> map = objectMapper.readValue( src, new TypeReference<>(){} ); Map<String, Object> spec = objectMapper.convertValue( Preconditions.checkNotNull(map.get("spec"),"spec not specified"), new TypeReference<>(){} ); DataSchema dataSchema = objectMapper.convertValue( Preconditions.checkNotNull(spec.get("dataSchema"), "dataschema not specified"), DataSchema.class ); Map<String, Object> ioConfig = objectMapper.convertValue( Preconditions.checkNotNull(spec.get("ioConfig"), "ioConfig not specified"), new TypeReference<>(){} ); InputSource inputSource0 = objectMapper.convertValue( Preconditions.checkNotNull(ioConfig.get("inputSource"), "inputSource not specified"), InputSource.class ); InputFormat inputFormat = objectMapper.convertValue( Preconditions.checkNotNull(ioConfig.get("inputFormat"), "inputFormat not specified"), InputFormat.class ); InputSource inputSource = relativizeLocalInputSource( inputSource0, ProjectPathUtils.PROJECT_ROOT ); TestDataSet dataset = new InputSourceBasedTestDataset( dataSchema, inputFormat, inputSource ); return dataset; } catch (IOException e) { throw new RuntimeException(e); } } public static TestDataSet makeDS3(ObjectMapper objectMapper, File src) { try { Map<String, Object> map = objectMapper.readValue( src, new TypeReference<>(){} ); Map<String, Object> spec = objectMapper.convertValue( Preconditions.checkNotNull(map.get("spec"),"spec not specified"), new TypeReference<>(){} ); DataSchema dataSchema = objectMapper.convertValue( Preconditions.checkNotNull(spec.get("dataSchema"), "dataschema not specified"), DataSchema.class ); ObjectMapper om = objectMapper.copy(); om.registerSubtypes(new NamedType(MyIOConfigType.class, "index_parallel")); MyIOConfigType ioConfig = om.convertValue( Preconditions.checkNotNull(spec.get("ioConfig"), "ioConfig not specified"), MyIOConfigType.class ); InputSource inputSource = relativizeLocalInputSource( ioConfig.inputSource, ProjectPathUtils.PROJECT_ROOT ); TestDataSet dataset = new InputSourceBasedTestDataset( dataSchema, ioConfig.inputFormat, inputSource ); return dataset; } catch (IOException e) { throw new RuntimeException(e); } } public static TestDataSet makeDS2(ObjectMapper objectMapper, File src) { try { Map<String, Object> map = objectMapper.readValue( src, new TypeReference<>() { } ); ObjectMapper om = objectMapper.copy(); om.registerSubtypes(new NamedType(MyIOConfigType.class, "index_parallel")); FakeIngestionSpec spec = om .convertValue(Preconditions.checkNotNull(map.get("spec"), "spec not specified"), FakeIngestionSpec.class); InputSource inputSource = relativizeLocalInputSource( spec.getIOConfig().inputSource, ProjectPathUtils.PROJECT_ROOT ); TestDataSet dataset = new InputSourceBasedTestDataset( spec.getDataSchema(), spec.getIOConfig().inputFormat, inputSource ); return dataset; } catch (IOException e) { throw new RuntimeException(e); } } public static TestDataSet makeDS1(ObjectMapper objectMapper, File src) { try { ObjectMapper om = objectMapper.copy(); om.registerSubtypes(new NamedType(MyIOConfigType.class, "index_parallel")); FakeIndexTask indexTask = om.readValue(src, FakeIndexTask.class); FakeIngestionSpec spec = indexTask.spec; InputSource inputSource = relativizeLocalInputSource( spec.getIOConfig().inputSource, ProjectPathUtils.PROJECT_ROOT ); TestDataSet dataset = new InputSourceBasedTestDataset( spec.getDataSchema(), spec.getIOConfig().inputFormat, inputSource ); return dataset; } catch (IOException e) { throw new RuntimeException(e); } } ``` </details> There are a few "middle" solutions which still use the `MyIoConfigType` ; but I think the cleanest is the original one - it might be harder to understand it at first - but if something will be missing later; it will be easier to adapt. There is also no need to prepare for missing values and such things as jackson will do the usual checks. -- 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: commits-unsubscr...@druid.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org For additional commands, e-mail: commits-h...@druid.apache.org