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.qubole.quark.fatjdbc;
import org.apache.calcite.DataContext;
import org.apache.calcite.avatica.AvaticaParameter;
import org.apache.calcite.avatica.AvaticaPreparedStatement;
import org.apache.calcite.avatica.AvaticaStatement;
import org.apache.calcite.avatica.AvaticaUtils;
import org.apache.calcite.avatica.ColumnMetaData;
import org.apache.calcite.avatica.Meta;
import org.apache.calcite.avatica.MetaImpl;
import org.apache.calcite.avatica.NoSuchStatementException;
import org.apache.calcite.avatica.QueryState;
import org.apache.calcite.avatica.SqlType;
import org.apache.calcite.avatica.remote.TypedValue;
import org.apache.calcite.jdbc.CalcitePrepare;
import org.apache.calcite.jdbc.CalciteSchema;
import org.apache.calcite.linq4j.Enumerable;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.linq4j.function.Function1;
import org.apache.calcite.linq4j.function.Functions;
import org.apache.calcite.linq4j.function.Predicate1;
import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactoryImpl;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.runtime.FlatLists;
import org.apache.calcite.schema.Table;
import org.apache.calcite.sql.SqlJdbcFunctionCall;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.Util;
import com.google.common.base.Preconditions;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.qubole.quark.fatjdbc.executor.PlanExecutor;
import com.qubole.quark.planner.parser.ParserResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
/**
* Created by rajatv on 10/27/15.
*/
public class QuarkMetaImpl extends MetaImpl {
private static final Logger LOG = LoggerFactory.getLogger(QuarkMetaImpl.class);
private static final String CONN_CACHE_KEY_BASE = "quark.connectioncache";
private static final String STMT_CACHE_KEY_BASE = "quark.statementcache";
/**
* Special value for {@link Statement#getMaxRows()} that means fetch
* an unlimited number of rows in a single batch.
*
* Any other negative value will return an unlimited number of rows but
* will do it in the default batch size, namely 100.
*/
public static final long UNLIMITED_COUNT = -2L;
public static final int QUERY_TIMEOUT = 60;
final Calendar calendar = Calendar.getInstance();
private final String url;
private final Properties info;
private final Cache connectionCache;
private final Cache statementCache;
/**
* Generates ids for statements. The ids are unique across all connections
* created by this JdbcMeta.
*/
private final AtomicInteger statementIdGenerator = new AtomicInteger();
public QuarkMetaImpl(QuarkConnectionImpl connection, Properties info) {
super(connection);
this.info = info;
this.url = info.getProperty("url");
int concurrencyLevel = Integer.parseInt(
info.getProperty(ConnectionCacheSettings.CONCURRENCY_LEVEL.key,
ConnectionCacheSettings.CONCURRENCY_LEVEL.defaultValue));
int initialCapacity = Integer.parseInt(
info.getProperty(ConnectionCacheSettings.INITIAL_CAPACITY.key,
ConnectionCacheSettings.INITIAL_CAPACITY.defaultValue));
long maxCapacity = Long.parseLong(
info.getProperty(ConnectionCacheSettings.MAX_CAPACITY.key,
ConnectionCacheSettings.MAX_CAPACITY.defaultValue));
long connectionExpiryDuration = Long.parseLong(
info.getProperty(ConnectionCacheSettings.EXPIRY_DURATION.key,
ConnectionCacheSettings.EXPIRY_DURATION.defaultValue));
TimeUnit connectionExpiryUnit = TimeUnit.valueOf(
info.getProperty(ConnectionCacheSettings.EXPIRY_UNIT.key,
ConnectionCacheSettings.EXPIRY_UNIT.defaultValue));
this.connectionCache = CacheBuilder.newBuilder()
.concurrencyLevel(concurrencyLevel)
.initialCapacity(initialCapacity)
.maximumSize(maxCapacity)
.expireAfterAccess(connectionExpiryDuration, connectionExpiryUnit)
.removalListener(new ConnectionExpiryHandler())
.build();
concurrencyLevel = Integer.parseInt(
info.getProperty(StatementCacheSettings.CONCURRENCY_LEVEL.key(),
StatementCacheSettings.CONCURRENCY_LEVEL.defaultValue()));
initialCapacity = Integer.parseInt(
info.getProperty(StatementCacheSettings.INITIAL_CAPACITY.key(),
StatementCacheSettings.INITIAL_CAPACITY.defaultValue()));
maxCapacity = Long.parseLong(
info.getProperty(StatementCacheSettings.MAX_CAPACITY.key(),
StatementCacheSettings.MAX_CAPACITY.defaultValue()));
connectionExpiryDuration = Long.parseLong(
info.getProperty(StatementCacheSettings.EXPIRY_DURATION.key(),
StatementCacheSettings.EXPIRY_DURATION.defaultValue()));
connectionExpiryUnit = TimeUnit.valueOf(
info.getProperty(StatementCacheSettings.EXPIRY_UNIT.key(),
StatementCacheSettings.EXPIRY_UNIT.defaultValue()));
this.statementCache = CacheBuilder.newBuilder()
.concurrencyLevel(concurrencyLevel)
.initialCapacity(initialCapacity)
.maximumSize(maxCapacity)
.expireAfterAccess(connectionExpiryDuration, connectionExpiryUnit)
.removalListener(new StatementExpiryHandler())
.build();
}
static Predicate1 namedMatcher(final Pat pattern) {
if (pattern.s == null || pattern.s.equals("%")) {
return Functions.truePredicate1();
}
final Pattern regex = likeToRegex(pattern);
return new Predicate1() {
public boolean apply(T v1) {
return regex.matcher(v1.getName()).matches();
}
};
}
static Predicate1 matcher(final Pat pattern) {
if (pattern.s == null || pattern.s.equals("%")) {
return Functions.truePredicate1();
}
final Pattern regex = likeToRegex(pattern);
return new Predicate1() {
public boolean apply(String v1) {
return regex.matcher(v1).matches();
}
};
}
/** Converts a LIKE-style pattern (where '%' represents a wild-card, escaped
* using '\') to a Java regex. */
public static Pattern likeToRegex(Pat pattern) {
StringBuilder buf = new StringBuilder("^");
char[] charArray = pattern.s.toCharArray();
int slash = -2;
for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
if (slash == i - 1) {
buf.append('[').append(c).append(']');
} else {
switch (c) {
case '\\':
slash = i;
break;
case '%':
buf.append(".*");
break;
case '[':
buf.append("\\[");
break;
case ']':
buf.append("\\]");
break;
default:
buf.append('[').append(c).append(']');
}
}
}
buf.append("$");
return Pattern.compile(buf.toString());
}
@Override
public StatementHandle createStatement(ConnectionHandle ch) {
final StatementHandle h = super.createStatement(ch);
final QuarkConnectionImpl quarkConnection = getConnection();
quarkConnection.server.addStatement(quarkConnection, h);
return h;
}
@Override
public void closeStatement(StatementHandle h) {
final QuarkConnectionImpl quarkConnection = getConnection();
QuarkJdbcStatement stmt = quarkConnection.server.getStatement(h);
// stmt.close(); // TODO: implement
quarkConnection.server.removeStatement(h);
}
private MetaResultSet createResultSet(Enumerable enumerable,
Class clazz, String... names) {
final List columns = new ArrayList<>();
final List fields = new ArrayList<>();
final List fieldNames = new ArrayList<>();
for (String name : names) {
final int index = fields.size();
final String fieldName = AvaticaUtils.toCamelCase(name);
final Field field;
try {
field = clazz.getField(fieldName);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
columns.add(columnMetaData(name, index, field.getType(), false));
fields.add(field);
fieldNames.add(fieldName);
}
//noinspection unchecked
final Iterable