soumyakanti3578 commented on code in PR #5131: URL: https://github.com/apache/hive/pull/5131#discussion_r1581522802
########## ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/RelPlanParser.java: ########## @@ -0,0 +1,477 @@ +/* + * 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.hadoop.hive.ql.optimizer.calcite; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableList; +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelTraitSet; +import org.apache.calcite.rel.RelCollation; +import org.apache.calcite.rel.RelCollationTraitDef; +import org.apache.calcite.rel.RelCollations; +import org.apache.calcite.rel.RelDistribution; +import org.apache.calcite.rel.RelInput; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.AggregateCall; +import org.apache.calcite.rel.core.JoinRelType; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.util.ImmutableBitSet; +import org.apache.calcite.util.Pair; +import org.apache.calcite.util.Util; +import org.apache.hadoop.hive.common.TableName; +import org.apache.hadoop.hive.ql.exec.ColumnInfo; +import org.apache.hadoop.hive.ql.metadata.VirtualColumn; +import org.apache.hadoop.hive.ql.optimizer.calcite.cost.HiveCostModel; +import org.apache.hadoop.hive.ql.optimizer.calcite.cost.HiveDefaultCostModel; +import org.apache.hadoop.hive.ql.optimizer.calcite.cost.HiveOnTezCostModel; +import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveGroupingID; +import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveRelNode; +import org.apache.hadoop.hive.ql.optimizer.calcite.translator.TypeConverter; +import org.apache.hadoop.hive.ql.parse.PrunedPartitionList; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.nio.charset.Charset; +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +import static org.apache.commons.lang3.StringUtils.isNotBlank; + +/** + * Class responsible for parsing a given plan from a json file. + */ +public class RelPlanParser { + private static final TypeReference<LinkedHashMap<String, Object>> TYPE_REF = + new TypeReference<LinkedHashMap<String, Object>>() { + }; + + private final RelOptCluster cluster; + private final RelOptHiveTableFactory relOptHiveTableFactory; + private final Map<String, PrunedPartitionList> partitionCache; + private final HiveRelJson relJson = new HiveRelJson(null); + private final Map<String, RelNode> relMap = new LinkedHashMap<>(); + private RelNode lastRel; + + public RelPlanParser(RelOptCluster cluster, + RelOptHiveTableFactory relOptHiveTableFactory, + Map<String, PrunedPartitionList> partitionCache) { + this.cluster = cluster; + this.relOptHiveTableFactory = relOptHiveTableFactory; + this.partitionCache = partitionCache; + } + + public RelNode parse(String json) throws IOException { + byte[] mapData = json.getBytes(Charset.defaultCharset()); + return read(new String(mapData, Charset.defaultCharset())); + } + + public RelNode read(String s) throws IOException { + JsonFactory jsonFactory = new JsonFactory(); + Map<String, Object> o; + try (JsonParser parser = jsonFactory.createParser(s)) { + parser.nextToken(); + assert parser.currentToken() == JsonToken.START_OBJECT; + parser.nextToken(); + assert parser.currentToken() == JsonToken.FIELD_NAME; + parser.nextToken(); + assert parser.currentToken() == JsonToken.VALUE_STRING; + + lastRel = null; + final ObjectMapper mapper = new ObjectMapper(); + o = mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true) + .readValue(parser.getValueAsString(), TYPE_REF); Review Comment: I think we need the elements in `Map<String, Object>` to use Calcite's `RelJson` which expects elements to be in `Map<String, Object>`. -- 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: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org