LakshSingla commented on code in PR #12386: URL: https://github.com/apache/druid/pull/12386#discussion_r862747709
########## sql/src/test/java/org/apache/druid/sql/calcite/CalciteIngestionDmlTest.java: ########## @@ -0,0 +1,339 @@ +/* + * 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; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.apache.druid.data.input.impl.CsvInputFormat; +import org.apache.druid.data.input.impl.InlineInputSource; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.granularity.Granularity; +import org.apache.druid.query.Query; +import org.apache.druid.query.QueryContext; +import org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.server.security.Action; +import org.apache.druid.server.security.AuthConfig; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.server.security.Resource; +import org.apache.druid.server.security.ResourceAction; +import org.apache.druid.server.security.ResourceType; +import org.apache.druid.sql.SqlLifecycle; +import org.apache.druid.sql.SqlLifecycleFactory; +import org.apache.druid.sql.calcite.external.ExternalDataSource; +import org.apache.druid.sql.calcite.parser.DruidSqlInsert; +import org.apache.druid.sql.calcite.planner.Calcites; +import org.apache.druid.sql.calcite.planner.PlannerConfig; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.util.CalciteTests; +import org.hamcrest.CoreMatchers; +import org.hamcrest.Matcher; +import org.hamcrest.MatcherAssert; +import org.junit.After; +import org.junit.Assert; +import org.junit.internal.matchers.ThrowableMessageMatcher; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class CalciteIngestionDmlTest extends BaseCalciteQueryTest +{ + protected static final Map<String, Object> DEFAULT_CONTEXT = + ImmutableMap.<String, Object>builder() + .put(PlannerContext.CTX_SQL_QUERY_ID, DUMMY_SQL_ID) + .build(); + + protected static final RowSignature FOO_TABLE_SIGNATURE = + RowSignature.builder() + .addTimeColumn() + .add("cnt", ColumnType.LONG) + .add("dim1", ColumnType.STRING) + .add("dim2", ColumnType.STRING) + .add("dim3", ColumnType.STRING) + .add("m1", ColumnType.FLOAT) + .add("m2", ColumnType.DOUBLE) + .add("unique_dim1", HyperUniquesAggregatorFactory.TYPE) + .build(); + + protected final ExternalDataSource externalDataSource = new ExternalDataSource( + new InlineInputSource("a,b,1\nc,d,2\n"), + new CsvInputFormat(ImmutableList.of("x", "y", "z"), null, false, false, 0), + RowSignature.builder() + .add("x", ColumnType.STRING) + .add("y", ColumnType.STRING) + .add("z", ColumnType.LONG) + .build() + ); + + protected boolean didTest = false; + + @After + @Override + public void tearDown() throws Exception + { + super.tearDown(); + + // Catch situations where tests forgot to call "verify" on their tester. + if (!didTest) { + throw new ISE("Test was not run; did you call verify() on a tester?"); + } + } + + protected String externSql(final ExternalDataSource externalDataSource) Review Comment: Can we extract this as a static method in the common class for the insert tests ########## sql/src/test/java/org/apache/druid/sql/calcite/CalciteIngestionDmlTest.java: ########## @@ -0,0 +1,339 @@ +/* + * 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; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.apache.druid.data.input.impl.CsvInputFormat; +import org.apache.druid.data.input.impl.InlineInputSource; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.granularity.Granularity; +import org.apache.druid.query.Query; +import org.apache.druid.query.QueryContext; +import org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.server.security.Action; +import org.apache.druid.server.security.AuthConfig; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.server.security.Resource; +import org.apache.druid.server.security.ResourceAction; +import org.apache.druid.server.security.ResourceType; +import org.apache.druid.sql.SqlLifecycle; +import org.apache.druid.sql.SqlLifecycleFactory; +import org.apache.druid.sql.calcite.external.ExternalDataSource; +import org.apache.druid.sql.calcite.parser.DruidSqlInsert; +import org.apache.druid.sql.calcite.planner.Calcites; +import org.apache.druid.sql.calcite.planner.PlannerConfig; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.util.CalciteTests; +import org.hamcrest.CoreMatchers; +import org.hamcrest.Matcher; +import org.hamcrest.MatcherAssert; +import org.junit.After; +import org.junit.Assert; +import org.junit.internal.matchers.ThrowableMessageMatcher; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class CalciteIngestionDmlTest extends BaseCalciteQueryTest +{ + protected static final Map<String, Object> DEFAULT_CONTEXT = + ImmutableMap.<String, Object>builder() + .put(PlannerContext.CTX_SQL_QUERY_ID, DUMMY_SQL_ID) + .build(); + + protected static final RowSignature FOO_TABLE_SIGNATURE = + RowSignature.builder() + .addTimeColumn() + .add("cnt", ColumnType.LONG) + .add("dim1", ColumnType.STRING) + .add("dim2", ColumnType.STRING) + .add("dim3", ColumnType.STRING) + .add("m1", ColumnType.FLOAT) + .add("m2", ColumnType.DOUBLE) + .add("unique_dim1", HyperUniquesAggregatorFactory.TYPE) + .build(); + + protected final ExternalDataSource externalDataSource = new ExternalDataSource( + new InlineInputSource("a,b,1\nc,d,2\n"), + new CsvInputFormat(ImmutableList.of("x", "y", "z"), null, false, false, 0), + RowSignature.builder() + .add("x", ColumnType.STRING) + .add("y", ColumnType.STRING) + .add("z", ColumnType.LONG) + .build() + ); + + protected boolean didTest = false; + + @After + @Override + public void tearDown() throws Exception + { + super.tearDown(); + + // Catch situations where tests forgot to call "verify" on their tester. + if (!didTest) { + throw new ISE("Test was not run; did you call verify() on a tester?"); + } + } + + protected String externSql(final ExternalDataSource externalDataSource) + { + try { + return StringUtils.format( + "TABLE(extern(%s, %s, %s))", + Calcites.escapeStringLiteral(queryJsonMapper.writeValueAsString(externalDataSource.getInputSource())), + Calcites.escapeStringLiteral(queryJsonMapper.writeValueAsString(externalDataSource.getInputFormat())), + Calcites.escapeStringLiteral(queryJsonMapper.writeValueAsString(externalDataSource.getSignature())) + ); + } + catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + protected Map<String, Object> queryContextWithGranularity(Granularity granularity) Review Comment: Can we extract this as a static method in the common class for the insert tests? (Similar comment as the above) ########## sql/src/test/java/org/apache/druid/sql/calcite/parser/DruidSqlUnparseTest.java: ########## @@ -0,0 +1,93 @@ +/* + * 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.parser; + +import org.apache.calcite.avatica.util.Casing; +import org.apache.calcite.sql.SqlWriter; +import org.apache.calcite.sql.dialect.CalciteSqlDialect; +import org.apache.calcite.sql.pretty.SqlPrettyWriter; +import org.junit.Test; + +import java.io.StringReader; + +import static org.junit.Assert.assertEquals; + +public class DruidSqlUnparseTest Review Comment: nit: I am not sure if we can improve the naming. But we should add a Javadoc here to denote the functionality of this test class. ########## sql/src/test/java/org/apache/druid/sql/calcite/CalciteReplaceDmlTest.java: ########## @@ -0,0 +1,608 @@ +/* + * 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; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.granularity.Granularities; +import org.apache.druid.java.util.common.granularity.Granularity; +import org.apache.druid.java.util.common.jackson.JacksonUtils; +import org.apache.druid.query.aggregation.CountAggregatorFactory; +import org.apache.druid.query.aggregation.LongSumAggregatorFactory; +import org.apache.druid.query.dimension.DefaultDimensionSpec; +import org.apache.druid.query.groupby.GroupByQuery; +import org.apache.druid.query.scan.ScanQuery; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.server.security.ForbiddenException; +import org.apache.druid.sql.SqlPlanningException; +import org.apache.druid.sql.calcite.external.ExternalOperatorConversion; +import org.apache.druid.sql.calcite.filtration.Filtration; +import org.apache.druid.sql.calcite.parser.DruidSqlInsert; +import org.apache.druid.sql.calcite.parser.DruidSqlReplace; +import org.apache.druid.sql.calcite.planner.PlannerConfig; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.util.CalciteTests; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class CalciteReplaceDmlTest extends CalciteIngestionDmlTest +{ + private static final Map<String, Object> REPLACE_ALL_TIME_CHUNKS = ImmutableMap.of( + DruidSqlInsert.SQL_INSERT_SEGMENT_GRANULARITY, + "{\"type\":\"all\"}", + DruidSqlReplace.SQL_REPLACE_TIME_CHUNKS, + "all" + ); + + @Override + protected Map<String, Object> queryContextWithGranularity(Granularity granularity) + { + String granularityString = null; + try { + granularityString = queryJsonMapper.writeValueAsString(granularity); + } + catch (JsonProcessingException e) { + Assert.fail(e.getMessage()); + } + return ImmutableMap.of( + DruidSqlInsert.SQL_INSERT_SEGMENT_GRANULARITY, granularityString, + DruidSqlReplace.SQL_REPLACE_TIME_CHUNKS, "all" Review Comment: Since there is more than one key that we are setting, the original name `withGranularity` doesn't seem right. We should specify what we are setting. Can this be done in two stages (with `SQL_INSERT_SEGMENT_GRANULARITY` populated in one - that could be common, and the `SQL_REPLACE_TIME_CHUNKS` in other). -- 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]
