Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 com.hazelcast.org.apache.calcite.rel.externalize;
import com.hazelcast.org.apache.calcite.avatica.AvaticaUtils;
import com.hazelcast.org.apache.calcite.avatica.util.TimeUnit;
import com.hazelcast.org.apache.calcite.plan.RelOptCluster;
import com.hazelcast.org.apache.calcite.plan.RelOptTable;
import com.hazelcast.org.apache.calcite.plan.RelTraitSet;
import com.hazelcast.org.apache.calcite.rel.RelCollation;
import com.hazelcast.org.apache.calcite.rel.RelCollationImpl;
import com.hazelcast.org.apache.calcite.rel.RelCollations;
import com.hazelcast.org.apache.calcite.rel.RelDistribution;
import com.hazelcast.org.apache.calcite.rel.RelDistributions;
import com.hazelcast.org.apache.calcite.rel.RelFieldCollation;
import com.hazelcast.org.apache.calcite.rel.RelFieldCollation.Direction;
import com.hazelcast.org.apache.calcite.rel.RelFieldCollation.NullDirection;
import com.hazelcast.org.apache.calcite.rel.RelInput;
import com.hazelcast.org.apache.calcite.rel.RelNode;
import com.hazelcast.org.apache.calcite.rel.core.AggregateCall;
import com.hazelcast.org.apache.calcite.rel.core.CorrelationId;
import com.hazelcast.org.apache.calcite.rel.type.RelDataType;
import com.hazelcast.org.apache.calcite.rel.type.RelDataTypeFactory;
import com.hazelcast.org.apache.calcite.rel.type.RelDataTypeField;
import com.hazelcast.org.apache.calcite.rex.RexBuilder;
import com.hazelcast.org.apache.calcite.rex.RexCall;
import com.hazelcast.org.apache.calcite.rex.RexCorrelVariable;
import com.hazelcast.org.apache.calcite.rex.RexFieldAccess;
import com.hazelcast.org.apache.calcite.rex.RexFieldCollation;
import com.hazelcast.org.apache.calcite.rex.RexLiteral;
import com.hazelcast.org.apache.calcite.rex.RexNode;
import com.hazelcast.org.apache.calcite.rex.RexOver;
import com.hazelcast.org.apache.calcite.rex.RexSlot;
import com.hazelcast.org.apache.calcite.rex.RexWindow;
import com.hazelcast.org.apache.calcite.rex.RexWindowBound;
import com.hazelcast.org.apache.calcite.rex.RexWindowBounds;
import com.hazelcast.org.apache.calcite.sql.SqlAggFunction;
import com.hazelcast.org.apache.calcite.sql.SqlFunction;
import com.hazelcast.org.apache.calcite.sql.SqlIdentifier;
import com.hazelcast.org.apache.calcite.sql.SqlIntervalQualifier;
import com.hazelcast.org.apache.calcite.sql.SqlKind;
import com.hazelcast.org.apache.calcite.sql.SqlOperator;
import com.hazelcast.org.apache.calcite.sql.SqlSyntax;
import com.hazelcast.org.apache.calcite.sql.fun.SqlStdOperatorTable;
import com.hazelcast.org.apache.calcite.sql.parser.SqlParserPos;
import com.hazelcast.org.apache.calcite.sql.type.SqlTypeName;
import com.hazelcast.org.apache.calcite.sql.validate.SqlNameMatchers;
import com.hazelcast.org.apache.calcite.util.ImmutableBitSet;
import com.hazelcast.org.apache.calcite.util.ImmutableIntList;
import com.hazelcast.org.apache.calcite.util.JsonBuilder;
import com.hazelcast.org.apache.calcite.util.Util;
import com.hazelcast.com.google.common.collect.ImmutableList;
import com.hazelcast.org.checkerframework.checker.nullness.qual.Nullable;
import com.hazelcast.org.checkerframework.checker.nullness.qual.PolyNull;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.hazelcast.org.apache.calcite.rel.RelDistributions.EMPTY;
import static com.hazelcast.org.apache.calcite.util.Static.RESOURCE;
import static java.util.Objects.requireNonNull;
/**
* Utilities for converting {@link com.hazelcast.org.apache.calcite.rel.RelNode}
* into JSON format.
*/
public class RelJson {
private final Map constructorMap = new HashMap<>();
private final @Nullable JsonBuilder jsonBuilder;
private final InputTranslator inputTranslator;
public static final List PACKAGES =
ImmutableList.of(
"com.hazelcast.org.apache.calcite.rel.",
"com.hazelcast.org.apache.calcite.rel.core.",
"com.hazelcast.org.apache.calcite.rel.logical.",
"com.hazelcast.org.apache.calcite.adapter.jdbc.",
"com.hazelcast.org.apache.calcite.adapter.jdbc.JdbcRules$");
/** Private constructor. */
private RelJson(@Nullable JsonBuilder jsonBuilder,
InputTranslator inputTranslator) {
this.jsonBuilder = jsonBuilder;
this.inputTranslator = requireNonNull(inputTranslator, "inputTranslator");
}
/** Creates a RelJson. */
public RelJson(@Nullable JsonBuilder jsonBuilder) {
this(jsonBuilder, RelJson::translateInput);
}
/** Returns a RelJson with a given InputTranslator. */
public RelJson withInputTranslator(InputTranslator inputTranslator) {
if (inputTranslator == this.inputTranslator) {
return this;
}
return new RelJson(jsonBuilder, inputTranslator);
}
private JsonBuilder jsonBuilder() {
return requireNonNull(jsonBuilder, "jsonBuilder");
}
@SuppressWarnings("unchecked")
private static T get(Map map,
String key) {
return (T) requireNonNull(map.get(key), () -> "entry for key " + key);
}
private static > T enumVal(Class clazz, Map map,
String key) {
String textValue = get(map, key);
return requireNonNull(
Util.enumVal(clazz, textValue),
() -> "unable to find enum value " + textValue + " in class " + clazz);
}
public RelNode create(Map map) {
String type = get(map, "type");
Constructor constructor = getConstructor(type);
try {
return (RelNode) constructor.newInstance(map);
} catch (InstantiationException | ClassCastException | InvocationTargetException
| IllegalAccessException e) {
throw new RuntimeException(
"while invoking constructor for type '" + type + "'", e);
}
}
public Constructor getConstructor(String type) {
Constructor constructor = constructorMap.get(type);
if (constructor == null) {
Class clazz = typeNameToClass(type);
try {
//noinspection unchecked
constructor = clazz.getConstructor(RelInput.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException("class does not have required constructor, "
+ clazz + "(RelInput)");
}
constructorMap.put(type, constructor);
}
return constructor;
}
/**
* Converts a type name to a class. E.g. {@code getClass("LogicalProject")}
* returns {@link com.hazelcast.org.apache.calcite.rel.logical.LogicalProject}.class.
*/
public Class typeNameToClass(String type) {
if (!type.contains(".")) {
for (String package_ : PACKAGES) {
try {
return Class.forName(package_ + type);
} catch (ClassNotFoundException e) {
// ignore
}
}
}
try {
return Class.forName(type);
} catch (ClassNotFoundException e) {
throw new RuntimeException("unknown type " + type);
}
}
/**
* Inverse of {@link #typeNameToClass}.
*/
public String classToTypeName(Class class_) {
final String canonicalName = class_.getName();
for (String package_ : PACKAGES) {
if (canonicalName.startsWith(package_)) {
String remaining = canonicalName.substring(package_.length());
if (remaining.indexOf('.') < 0 && remaining.indexOf('$') < 0) {
return remaining;
}
}
}
return canonicalName;
}
/** Default implementation of
* {@link InputTranslator#translateInput(RelJson, int, Map, RelInput)}. */
private static RexNode translateInput(RelJson relJson, int input,
Map map, RelInput relInput) {
final RelOptCluster cluster = relInput.getCluster();
final RexBuilder rexBuilder = cluster.getRexBuilder();
// Check if it is a local ref.
if (map.containsKey("type")) {
final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
final RelDataType type = relJson.toType(typeFactory, get(map, "type"));
return rexBuilder.makeLocalRef(type, input);
}
int i = input;
final List relNodes = relInput.getInputs();
for (RelNode inputNode : relNodes) {
final RelDataType rowType = inputNode.getRowType();
if (i < rowType.getFieldCount()) {
final RelDataTypeField field = rowType.getFieldList().get(i);
return rexBuilder.makeInputRef(field.getType(), input);
}
i -= rowType.getFieldCount();
}
throw new RuntimeException("input field " + input + " is out of range");
}
public Object toJson(RelCollationImpl node) {
final List