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 org.apache.calcite.schema;
import org.apache.calcite.DataContext;
import org.apache.calcite.adapter.java.JavaTypeFactory;
import org.apache.calcite.config.CalciteConnectionConfig;
import org.apache.calcite.config.CalciteConnectionConfigImpl;
import org.apache.calcite.config.CalciteConnectionProperty;
import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.jdbc.CalcitePrepare;
import org.apache.calcite.jdbc.CalciteSchema;
import org.apache.calcite.linq4j.Enumerable;
import org.apache.calcite.linq4j.QueryProvider;
import org.apache.calcite.linq4j.Queryable;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
import org.apache.calcite.linq4j.tree.MethodCallExpression;
import org.apache.calcite.linq4j.tree.Types;
import org.apache.calcite.materialize.Lattice;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelProtoDataType;
import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.tools.RelRunner;
import org.apache.calcite.util.BuiltInMethod;
import org.apache.calcite.util.Pair;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static org.apache.calcite.jdbc.CalciteSchema.LatticeEntry;
/**
* Utility functions for schemas.
*/
public final class Schemas {
private Schemas() {
throw new AssertionError("no instances!");
}
public static CalciteSchema.FunctionEntry resolve(
RelDataTypeFactory typeFactory,
String name,
Collection functionEntries,
List argumentTypes) {
final List matches = new ArrayList<>();
for (CalciteSchema.FunctionEntry entry : functionEntries) {
if (matches(typeFactory, entry.getFunction(), argumentTypes)) {
matches.add(entry);
}
}
switch (matches.size()) {
case 0:
return null;
case 1:
return matches.get(0);
default:
throw new RuntimeException("More than one match for " + name
+ " with arguments " + argumentTypes);
}
}
private static boolean matches(RelDataTypeFactory typeFactory,
Function member, List argumentTypes) {
List parameters = member.getParameters();
if (parameters.size() != argumentTypes.size()) {
return false;
}
for (int i = 0; i < argumentTypes.size(); i++) {
RelDataType argumentType = argumentTypes.get(i);
FunctionParameter parameter = parameters.get(i);
if (!canConvert(argumentType, parameter.getType(typeFactory))) {
return false;
}
}
return true;
}
private static boolean canConvert(RelDataType fromType, RelDataType toType) {
return SqlTypeUtil.canAssignFrom(toType, fromType);
}
/** Returns the expression for a schema. */
public static Expression expression(SchemaPlus schema) {
return schema.getExpression(schema.getParentSchema(), schema.getName());
}
/** Returns the expression for a sub-schema. */
public static Expression subSchemaExpression(SchemaPlus schema, String name,
Class type) {
// (Type) schemaExpression.getSubSchema("name")
final Expression schemaExpression = expression(schema);
Expression call =
Expressions.call(
schemaExpression,
BuiltInMethod.SCHEMA_GET_SUB_SCHEMA.method,
Expressions.constant(name));
//CHECKSTYLE: IGNORE 2
//noinspection unchecked
if (false && type != null && !type.isAssignableFrom(Schema.class)) {
return unwrap(call, type);
}
return call;
}
/** Converts a schema expression to a given type by calling the
* {@link SchemaPlus#unwrap(Class)} method. */
public static Expression unwrap(Expression call, Class type) {
return Expressions.convert_(
Expressions.call(call, BuiltInMethod.SCHEMA_PLUS_UNWRAP.method,
Expressions.constant(type)),
type);
}
/** Returns the expression to access a table within a schema. */
public static Expression tableExpression(SchemaPlus schema, Type elementType,
String tableName, Class clazz) {
final MethodCallExpression expression;
if (Table.class.isAssignableFrom(clazz)) {
expression = Expressions.call(
expression(schema),
BuiltInMethod.SCHEMA_GET_TABLE.method,
Expressions.constant(tableName));
if (ScannableTable.class.isAssignableFrom(clazz)) {
return Expressions.call(
BuiltInMethod.SCHEMAS_ENUMERABLE_SCANNABLE.method,
Expressions.convert_(expression, ScannableTable.class),
DataContext.ROOT);
}
if (FilterableTable.class.isAssignableFrom(clazz)) {
return Expressions.call(
BuiltInMethod.SCHEMAS_ENUMERABLE_FILTERABLE.method,
Expressions.convert_(expression, FilterableTable.class),
DataContext.ROOT);
}
if (ProjectableFilterableTable.class.isAssignableFrom(clazz)) {
return Expressions.call(
BuiltInMethod.SCHEMAS_ENUMERABLE_PROJECTABLE_FILTERABLE.method,
Expressions.convert_(expression, ProjectableFilterableTable.class),
DataContext.ROOT);
}
} else {
expression = Expressions.call(
BuiltInMethod.SCHEMAS_QUERYABLE.method,
DataContext.ROOT,
expression(schema),
Expressions.constant(elementType),
Expressions.constant(tableName));
}
return Types.castIfNecessary(clazz, expression);
}
public static DataContext createDataContext(
Connection connection, SchemaPlus rootSchema) {
return new DummyDataContext((CalciteConnection) connection, rootSchema);
}
/** Returns a {@link Queryable}, given a fully-qualified table name. */
public static Queryable queryable(DataContext root, Class clazz,
String... names) {
return queryable(root, clazz, Arrays.asList(names));
}
/** Returns a {@link Queryable}, given a fully-qualified table name as an
* iterable. */
public static Queryable queryable(DataContext root, Class clazz,
Iterable extends String> names) {
SchemaPlus schema = root.getRootSchema();
for (Iterator extends String> iterator = names.iterator();;) {
String name = iterator.next();
if (iterator.hasNext()) {
schema = schema.getSubSchema(name);
} else {
return queryable(root, schema, clazz, name);
}
}
}
/** Returns a {@link Queryable}, given a schema and table name. */
public static Queryable queryable(DataContext root, SchemaPlus schema,
Class clazz, String tableName) {
QueryableTable table = (QueryableTable) schema.getTable(tableName);
return table.asQueryable(root.getQueryProvider(), schema, tableName);
}
/** Returns an {@link org.apache.calcite.linq4j.Enumerable} over the rows of
* a given table, representing each row as an object array. */
public static Enumerable