ccaominh commented on a change in pull request #6974: sql support for dynamic parameters URL: https://github.com/apache/incubator-druid/pull/6974#discussion_r311328062
########## File path: sql/src/main/java/org/apache/druid/sql/calcite/planner/SqlParametizerShuttle.java ########## @@ -0,0 +1,73 @@ +/* + * 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.planner; + +import org.apache.calcite.avatica.remote.TypedValue; +import org.apache.calcite.sql.SqlDynamicParam; +import org.apache.calcite.sql.SqlLiteral; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.sql.util.SqlShuttle; +import org.apache.calcite.util.TimestampString; + +/** + * Replaces all {@link SqlDynamicParam} encountered in an {@link SqlNode} tree with a {@link SqlLiteral} if a value + * binding exists for the parameter, if possible. This is used in tandem with {@link RelParameterizerShuttle}. + * + * It is preferable that all parameters are placed here to pick up as many optimizations as possible, but the facilities + * to convert jdbc types to {@link SqlLiteral} are a bit less rich here than exist for converting a + * {@link org.apache.calcite.rex.RexDynamicParam} to {@link org.apache.calcite.rex.RexLiteral}, which is why + * {@link SqlParametizerShuttle} and {@link RelParameterizerShuttle} both exist. + */ +public class SqlParametizerShuttle extends SqlShuttle +{ + private final PlannerContext plannerContext; + + public SqlParametizerShuttle(PlannerContext plannerContext) + { + this.plannerContext = plannerContext; + } + + @Override + public SqlNode visit(SqlDynamicParam param) + { + try { + if (plannerContext.getParameters().size() > param.getIndex()) { + TypedValue paramBinding = plannerContext.getParameters().get(param.getIndex()); + SqlTypeName typeName = SqlTypeName.getNameForJdbcType(paramBinding.type.typeId); + if (SqlTypeName.APPROX_TYPES.contains(typeName)) { + return SqlLiteral.createApproxNumeric(paramBinding.value.toString(), param.getParserPosition()); + } + if (SqlTypeName.TIMESTAMP.equals(typeName) && paramBinding.value instanceof Long) { Review comment: Do you need similar logic for the `DATE` type as well? May be useful to have a `SqlParametizerTest` class with the respective unit tests for the different dynamic parameter types. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org For additional commands, e-mail: commits-h...@druid.apache.org