All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.org.apache.calcite.jdbc.CalciteMetaImpl Maven / Gradle / Ivy

/*
 * 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.jdbc;

import com.hazelcast.org.apache.calcite.DataContext;
import com.hazelcast.org.apache.calcite.adapter.java.AbstractQueryableTable;
import com.hazelcast.org.apache.calcite.adapter.java.JavaTypeFactory;
import com.hazelcast.org.apache.calcite.avatica.AvaticaStatement;
import com.hazelcast.org.apache.calcite.avatica.AvaticaUtils;
import com.hazelcast.org.apache.calcite.avatica.ColumnMetaData;
import com.hazelcast.org.apache.calcite.avatica.Meta;
import com.hazelcast.org.apache.calcite.avatica.MetaImpl;
import com.hazelcast.org.apache.calcite.avatica.NoSuchStatementException;
import com.hazelcast.org.apache.calcite.avatica.QueryState;
import com.hazelcast.org.apache.calcite.avatica.remote.TypedValue;
import com.hazelcast.org.apache.calcite.jdbc.CalcitePrepare.Context;
import com.hazelcast.org.apache.calcite.linq4j.Enumerable;
import com.hazelcast.org.apache.calcite.linq4j.Enumerator;
import com.hazelcast.org.apache.calcite.linq4j.Linq4j;
import com.hazelcast.org.apache.calcite.linq4j.QueryProvider;
import com.hazelcast.org.apache.calcite.linq4j.Queryable;
import com.hazelcast.org.apache.calcite.linq4j.function.Function1;
import com.hazelcast.org.apache.calcite.linq4j.function.Functions;
import com.hazelcast.org.apache.calcite.linq4j.function.Predicate1;
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.RelDataTypeFactoryImpl;
import com.hazelcast.org.apache.calcite.rel.type.RelDataTypeSystem;
import com.hazelcast.org.apache.calcite.runtime.FlatLists;
import com.hazelcast.org.apache.calcite.runtime.Hook;
import com.hazelcast.org.apache.calcite.schema.Schema;
import com.hazelcast.org.apache.calcite.schema.SchemaPlus;
import com.hazelcast.org.apache.calcite.schema.Table;
import com.hazelcast.org.apache.calcite.schema.impl.AbstractTableQueryable;
import com.hazelcast.org.apache.calcite.schema.impl.MaterializedViewTable;
import com.hazelcast.org.apache.calcite.server.CalciteServerStatement;
import com.hazelcast.org.apache.calcite.sql.SqlJdbcFunctionCall;
import com.hazelcast.org.apache.calcite.sql.SqlKind;
import com.hazelcast.org.apache.calcite.sql.SqlOperator;
import com.hazelcast.org.apache.calcite.sql.SqlOperatorTable;
import com.hazelcast.org.apache.calcite.sql.fun.SqlStdOperatorTable;
import com.hazelcast.org.apache.calcite.sql.parser.SqlParser;
import com.hazelcast.org.apache.calcite.sql.type.SqlTypeName;
import com.hazelcast.org.apache.calcite.tools.FrameworkConfig;
import com.hazelcast.org.apache.calcite.tools.Frameworks;
import com.hazelcast.org.apache.calcite.util.Holder;
import com.hazelcast.org.apache.calcite.util.Pair;
import com.hazelcast.org.apache.calcite.util.Util;

import com.hazelcast.com.google.common.annotations.VisibleForTesting;
import com.hazelcast.com.google.common.collect.ImmutableList;
import com.hazelcast.com.google.common.collect.ImmutableMap;
import com.hazelcast.com.google.common.primitives.Ints;
import com.hazelcast.com.google.common.primitives.Longs;

import com.hazelcast.org.checkerframework.checker.nullness.qual.Nullable;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static java.util.Objects.requireNonNull;

/**
 * Helper for implementing the {@code getXxx} methods such as
 * {@link com.hazelcast.org.apache.calcite.avatica.AvaticaDatabaseMetaData#getTables}.
 */
public class CalciteMetaImpl extends MetaImpl {
  static final Driver DRIVER = new Driver();

  public CalciteMetaImpl(CalciteConnectionImpl connection) {
    super(connection);
    this.connProps
        .setAutoCommit(false)
        .setReadOnly(false)
        .setTransactionIsolation(Connection.TRANSACTION_NONE);
    this.connProps.setDirty(false);
  }

  static  Predicate1 namedMatcher(final Pat pattern) {
    if (pattern.s == null || pattern.s.equals("%")) {
      return Functions.truePredicate1();
    }
    final Pattern regex = likeToRegex(pattern);
    return v1 -> 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 v1 -> 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 CalciteConnectionImpl calciteConnection = getConnection();
    calciteConnection.server.addStatement(calciteConnection, h);
    return h;
  }

  @Override public void closeStatement(StatementHandle h) {
    final CalciteConnectionImpl calciteConnection = getConnection();
    @SuppressWarnings("unused")
    final CalciteServerStatement stmt;
    try {
      stmt = calciteConnection.server.getStatement(h);
    } catch (NoSuchStatementException e) {
      // statement is not valid; nothing to do
      return;
    }
    // stmt.close(); // TODO: implement
    calciteConnection.server.removeStatement(h);
  }

  private  MetaResultSet createResultSet(Enumerable enumerable,
      Class clazz, String... names) {
    requireNonNull(names, "names");
    final List columns = new ArrayList<>(names.length);
    final List fields = new ArrayList<>(names.length);
    final List fieldNames = new ArrayList<>(names.length);
    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 iterable = (Iterable) (Iterable) enumerable;
    return createResultSet(Collections.emptyMap(),
        columns, CursorFactory.record(clazz, fields, fieldNames),
        new Frame(0, true, iterable));
  }

  @Override protected MetaResultSet createResultSet(
      Map internalParameters, List columns,
      CursorFactory cursorFactory, final Frame firstFrame) {
    try {
      final CalciteConnectionImpl connection = getConnection();
      final AvaticaStatement statement = connection.createStatement();
      final CalcitePrepare.CalciteSignature signature =
          new CalcitePrepare.CalciteSignature("",
              ImmutableList.of(), internalParameters, null,
              columns, cursorFactory, null, ImmutableList.of(), -1,
              null, Meta.StatementType.SELECT) {
            @Override public Enumerable enumerable(
                DataContext dataContext) {
              return Linq4j.asEnumerable(firstFrame.rows);
            }
          };
      return MetaResultSet.create(connection.id, statement.getId(), true,
          signature, firstFrame);
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
  }

  CalciteConnectionImpl getConnection() {
    return (CalciteConnectionImpl) connection;
  }

  @Override public Map getDatabaseProperties(ConnectionHandle ch) {
    final ImmutableMap.Builder builder =
        ImmutableMap.builder();
    for (DatabaseProperty p : DatabaseProperty.values()) {
      addProperty(builder, p);
    }
    return builder.build();
  }

  private static ImmutableMap.Builder addProperty(
      ImmutableMap.Builder builder,
      DatabaseProperty p) {
    switch (p) {
    case GET_S_Q_L_KEYWORDS:
      return builder.put(p,
          SqlParser.create("").getMetadata().getJdbcKeywords());
    case GET_NUMERIC_FUNCTIONS:
      return builder.put(p, SqlJdbcFunctionCall.getNumericFunctions());
    case GET_STRING_FUNCTIONS:
      return builder.put(p, SqlJdbcFunctionCall.getStringFunctions());
    case GET_SYSTEM_FUNCTIONS:
      return builder.put(p, SqlJdbcFunctionCall.getSystemFunctions());
    case GET_TIME_DATE_FUNCTIONS:
      return builder.put(p, SqlJdbcFunctionCall.getTimeDateFunctions());
    default:
      return builder;
    }
  }

  @Override public MetaResultSet getTables(ConnectionHandle ch,
      String catalog,
      final Pat schemaPattern,
      final Pat tableNamePattern,
      final List typeList) {
    final Predicate1 typeFilter;
    if (typeList == null) {
      typeFilter = Functions.truePredicate1();
    } else {
      typeFilter = v1 -> typeList.contains(v1.tableType);
    }
    final Predicate1 schemaMatcher = namedMatcher(schemaPattern);
    return createResultSet(schemas(catalog)
            .where(schemaMatcher)
            .selectMany(schema -> tables(schema, matcher(tableNamePattern)))
            .where(typeFilter),
        MetaTable.class,
        "TABLE_CAT",
        "TABLE_SCHEM",
        "TABLE_NAME",
        "TABLE_TYPE",
        "REMARKS",
        "TYPE_CAT",
        "TYPE_SCHEM",
        "TYPE_NAME",
        "SELF_REFERENCING_COL_NAME",
        "REF_GENERATION");
  }

  @Override public MetaResultSet getTypeInfo(ConnectionHandle ch) {
    return createResultSet(allTypeInfo(),
        MetaTypeInfo.class,
        "TYPE_NAME",
        "DATA_TYPE",
        "PRECISION",
        "LITERAL_PREFIX",
        "LITERAL_SUFFIX",
        "CREATE_PARAMS",
        "NULLABLE",
        "CASE_SENSITIVE",
        "SEARCHABLE",
        "UNSIGNED_ATTRIBUTE",
        "FIXED_PREC_SCALE",
        "AUTO_INCREMENT",
        "LOCAL_TYPE_NAME",
        "MINIMUM_SCALE",
        "MAXIMUM_SCALE",
        "SQL_DATA_TYPE",
        "SQL_DATETIME_SUB",
        "NUM_PREC_RADIX");
  }

  @Override public MetaResultSet getColumns(ConnectionHandle ch,
      String catalog,
      Pat schemaPattern,
      Pat tableNamePattern,
      Pat columnNamePattern) {
    final Predicate1 tableNameMatcher = matcher(tableNamePattern);
    final Predicate1 schemaMatcher = namedMatcher(schemaPattern);
    final Predicate1 columnMatcher =
        namedMatcher(columnNamePattern);
    return createResultSet(schemas(catalog)
            .where(schemaMatcher)
            .selectMany(schema -> tables(schema, tableNameMatcher))
            .selectMany(this::columns)
            .where(columnMatcher),
        MetaColumn.class,
        "TABLE_CAT",
        "TABLE_SCHEM",
        "TABLE_NAME",
        "COLUMN_NAME",
        "DATA_TYPE",
        "TYPE_NAME",
        "COLUMN_SIZE",
        "BUFFER_LENGTH",
        "DECIMAL_DIGITS",
        "NUM_PREC_RADIX",
        "NULLABLE",
        "REMARKS",
        "COLUMN_DEF",
        "SQL_DATA_TYPE",
        "SQL_DATETIME_SUB",
        "CHAR_OCTET_LENGTH",
        "ORDINAL_POSITION",
        "IS_NULLABLE",
        "SCOPE_CATALOG",
        "SCOPE_SCHEMA",
        "SCOPE_TABLE",
        "SOURCE_DATA_TYPE",
        "IS_AUTOINCREMENT",
        "IS_GENERATEDCOLUMN");
  }

  Enumerable catalogs() {
    final String catalog;
    try {
      catalog = connection.getCatalog();
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
    return Linq4j.asEnumerable(
        ImmutableList.of(new MetaCatalog(catalog)));
  }

  Enumerable tableTypes() {
    return Linq4j.asEnumerable(
        ImmutableList.of(
            new MetaTableType("TABLE"), new MetaTableType("VIEW")));
  }

  Enumerable schemas(final String catalog) {
    return Linq4j.asEnumerable(
        getConnection().rootSchema.getSubSchemaMap().values())
        .select((Function1) calciteSchema ->
            new CalciteMetaSchema(calciteSchema, catalog,
                calciteSchema.getName()))
        .orderBy((Function1) metaSchema ->
            (Comparable) FlatLists.of(Util.first(metaSchema.tableCatalog, ""),
                metaSchema.tableSchem));
  }

  Enumerable tables(String catalog) {
    return schemas(catalog)
        .selectMany(schema ->
            tables(schema, Functions.truePredicate1()));
  }

  Enumerable tables(final MetaSchema schema_) {
    final CalciteMetaSchema schema = (CalciteMetaSchema) schema_;
    return Linq4j.asEnumerable(schema.calciteSchema.getTableNames())
        .select((Function1) name -> {
          final Table table = requireNonNull(
              schema.calciteSchema.getTable(name, true),
              () -> "table " + name + " is not found (case sensitive)").getTable();
          return new CalciteMetaTable(table,
              schema.tableCatalog,
              schema.tableSchem,
              name);
        })
        .concat(
            Linq4j.asEnumerable(
                schema.calciteSchema.getTablesBasedOnNullaryFunctions()
                    .entrySet())
                .select(pair -> {
                  final Table table = pair.getValue();
                  return new CalciteMetaTable(table,
                      schema.tableCatalog,
                      schema.tableSchem,
                      pair.getKey());
                }));
  }

  Enumerable tables(
      final MetaSchema schema,
      final Predicate1 matcher) {
    return tables(schema)
        .where(v1 -> matcher.apply(v1.getName()));
  }

  private ImmutableList getAllDefaultType() {
    final ImmutableList.Builder allTypeList =
        ImmutableList.builder();
    final CalciteConnectionImpl conn = (CalciteConnectionImpl) connection;
    final RelDataTypeSystem typeSystem = conn.typeFactory.getTypeSystem();
    for (SqlTypeName sqlTypeName : SqlTypeName.values()) {
      if (sqlTypeName.isSpecial()) {
        // Skip internal types (NULL, ANY, SYMBOL, SARG).
        continue;
      }
      allTypeList.add(
          new MetaTypeInfo(sqlTypeName.getName(),
              sqlTypeName.getJdbcOrdinal(),
              typeSystem.getMaxPrecision(sqlTypeName),
              typeSystem.getLiteral(sqlTypeName, true),
              typeSystem.getLiteral(sqlTypeName, false),
              // All types are nullable
              (short) DatabaseMetaData.typeNullable,
              typeSystem.isCaseSensitive(sqlTypeName),
              // Making all type searchable; we may want to
              // be specific and declare under SqlTypeName
              (short) DatabaseMetaData.typeSearchable,
              false,
              false,
              typeSystem.isAutoincrement(sqlTypeName),
              (short) sqlTypeName.getMinScale(),
              (short) typeSystem.getMaxScale(sqlTypeName),
              typeSystem.getNumTypeRadix(sqlTypeName)));
    }
    return allTypeList.build();
  }

  protected Enumerable allTypeInfo() {
    return Linq4j.asEnumerable(getAllDefaultType());
  }

  public Enumerable columns(final MetaTable table_) {
    final CalciteMetaTable table = (CalciteMetaTable) table_;
    final RelDataType rowType =
        table.calciteTable.getRowType(getConnection().typeFactory);
    return Linq4j.asEnumerable(rowType.getFieldList())
        .select(field -> {
          final int precision =
              field.getType().getSqlTypeName().allowsPrec()
                  && !(field.getType()
                  instanceof RelDataTypeFactoryImpl.JavaType)
                  ? field.getType().getPrecision()
                  : -1;
          return new MetaColumn(
              table.tableCat,
              table.tableSchem,
              table.tableName,
              field.getName(),
              field.getType().getSqlTypeName().getJdbcOrdinal(),
              field.getType().getFullTypeString(),
              precision,
              field.getType().getSqlTypeName().allowsScale()
                  ? field.getType().getScale()
                  : null,
              10,
              field.getType().isNullable()
                  ? DatabaseMetaData.columnNullable
                  : DatabaseMetaData.columnNoNulls,
              precision,
              field.getIndex() + 1,
              field.getType().isNullable() ? "YES" : "NO");
        });
  }

  @Override public MetaResultSet getSchemas(ConnectionHandle ch, String catalog,
      Pat schemaPattern) {
    final Predicate1 schemaMatcher = namedMatcher(schemaPattern);
    return createResultSet(schemas(catalog).where(schemaMatcher),
        MetaSchema.class,
        "TABLE_SCHEM",
        "TABLE_CATALOG");
  }

  @Override public MetaResultSet getCatalogs(ConnectionHandle ch) {
    return createResultSet(catalogs(),
        MetaCatalog.class,
        "TABLE_CAT");
  }

  @Override public MetaResultSet getTableTypes(ConnectionHandle ch) {
    return createResultSet(tableTypes(),
        MetaTableType.class,
        "TABLE_TYPE");
  }

  @Override public MetaResultSet getFunctions(ConnectionHandle ch,
      String catalog,
      Pat schemaPattern,
      Pat functionNamePattern) {
    final Predicate1 schemaMatcher = namedMatcher(schemaPattern);
    return createResultSet(schemas(catalog)
            .where(schemaMatcher)
            .selectMany(schema -> functions(schema, catalog, matcher(functionNamePattern)))
            .orderBy(x ->
                (Comparable) FlatLists.of(
                    x.functionCat, x.functionSchem, x.functionName, x.specificName
                )),
        MetaFunction.class,
        "FUNCTION_CAT",
        "FUNCTION_SCHEM",
        "FUNCTION_NAME",
        "REMARKS",
        "FUNCTION_TYPE",
        "SPECIFIC_NAME");
  }

  Enumerable functions(final MetaSchema schema_, final String catalog) {
    final CalciteMetaSchema schema = (CalciteMetaSchema) schema_;
    Enumerable opTableFunctions = Linq4j.emptyEnumerable();
    if (schema.calciteSchema.schema.equals(MetadataSchema.INSTANCE)) {
      SqlOperatorTable opTable = getConnection().config()
          .fun(SqlOperatorTable.class, SqlStdOperatorTable.instance());
      List q = opTable.getOperatorList();
      opTableFunctions = Linq4j.asEnumerable(q)
          .where(op -> SqlKind.FUNCTION.contains(op.getKind()))
          .select(op ->
              new MetaFunction(
                  catalog,
                  schema.getName(),
                  op.getName(),
                  (short) DatabaseMetaData.functionResultUnknown,
                  op.getName()
              )
          );
    }
    return Linq4j.asEnumerable(schema.calciteSchema.getFunctionNames())
        .selectMany(name ->
            Linq4j.asEnumerable(schema.calciteSchema.getFunctions(name, true))
                //exclude materialized views from the result set
                .where(fn -> !(fn instanceof MaterializedViewTable.MaterializedViewTableMacro))
                .select(fnx ->
                    new MetaFunction(
                        catalog,
                        schema.getName(),
                        name,
                        (short) DatabaseMetaData.functionResultUnknown,
                        name
                    )
                )
        )
        .concat(opTableFunctions);
  }

  Enumerable functions(final MetaSchema schema, final String catalog,
      final Predicate1 functionNameMatcher) {
    return functions(schema, catalog)
        .where(v1 -> functionNameMatcher.apply(v1.functionName));
  }

  @Override public Iterable createIterable(StatementHandle handle, QueryState state,
      Signature signature, @Nullable List parameterValues, @Nullable Frame firstFrame) {
    // Drop QueryState
    return _createIterable(handle, signature, parameterValues, firstFrame);
  }

  Iterable _createIterable(StatementHandle handle,
      Signature signature, @Nullable List parameterValues, @Nullable Frame firstFrame) {
    try {
      //noinspection unchecked
      final CalcitePrepare.CalciteSignature calciteSignature =
          (CalcitePrepare.CalciteSignature) signature;
      return getConnection().enumerable(handle, calciteSignature, parameterValues);
    } catch (SQLException e) {
      throw new RuntimeException(e.getMessage());
    }
  }

  @Override public StatementHandle prepare(ConnectionHandle ch, String sql,
      long maxRowCount) {
    final StatementHandle h = createStatement(ch);
    final CalciteConnectionImpl calciteConnection = getConnection();

    final CalciteServerStatement statement;
    try {
      statement = calciteConnection.server.getStatement(h);
    } catch (NoSuchStatementException e) {
      // Not possible. We just created a statement.
      throw new AssertionError("missing statement", e);
    }
    final Context context = statement.createPrepareContext();
    final CalcitePrepare.Query query = toQuery(context, sql);
    h.signature = calciteConnection.parseQuery(query, context, maxRowCount);
    statement.setSignature(h.signature);
    return h;
  }

  @SuppressWarnings("deprecation")
  @Override public ExecuteResult prepareAndExecute(StatementHandle h,
      String sql, long maxRowCount, PrepareCallback callback)
      throws NoSuchStatementException {
    return prepareAndExecute(h, sql, maxRowCount, -1, callback);
  }

  @Override public ExecuteResult prepareAndExecute(StatementHandle h,
      String sql, long maxRowCount, int maxRowsInFirstFrame,
      PrepareCallback callback) throws NoSuchStatementException {
    final CalcitePrepare.CalciteSignature signature;
    try {
      final int updateCount;
      synchronized (callback.getMonitor()) {
        callback.clear();
        final CalciteConnectionImpl calciteConnection = getConnection();
        final CalciteServerStatement statement =
            calciteConnection.server.getStatement(h);
        final Context context = statement.createPrepareContext();
        final CalcitePrepare.Query query = toQuery(context, sql);
        signature = calciteConnection.parseQuery(query, context, maxRowCount);
        statement.setSignature(signature);
        switch (signature.statementType) {
        case CREATE:
        case DROP:
        case ALTER:
        case OTHER_DDL:
          updateCount = 0; // DDL produces no result set
          break;
        default:
          updateCount = -1; // SELECT and DML produces result set
          break;
        }
        callback.assign(signature, null, updateCount);
      }
      callback.execute();
      final MetaResultSet metaResultSet =
          MetaResultSet.create(h.connectionId, h.id, false, signature, null, updateCount);
      return new ExecuteResult(ImmutableList.of(metaResultSet));
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
    // TODO: share code with prepare and createIterable
  }

  /** Wraps the SQL string in a
   * {@link com.hazelcast.org.apache.calcite.jdbc.CalcitePrepare.Query} object, giving the
   * {@link Hook#STRING_TO_QUERY} hook chance to override. */
  private static CalcitePrepare.Query toQuery(
          Context context, String sql) {
    final Holder> queryHolder =
        Holder.of(CalcitePrepare.Query.of(sql));
    final FrameworkConfig config = Frameworks.newConfigBuilder()
        .parserConfig(SqlParser.Config.DEFAULT)
        .defaultSchema(context.getRootSchema().plus())
        .build();
    Hook.STRING_TO_QUERY.run(Pair.of(config, queryHolder));
    return queryHolder.get();
  }

  @Override public Frame fetch(StatementHandle h, long offset,
      int fetchMaxRowCount) throws NoSuchStatementException {
    final CalciteConnectionImpl calciteConnection = getConnection();
    CalciteServerStatement stmt = calciteConnection.server.getStatement(h);
    final Signature signature = requireNonNull(stmt.getSignature(),
        () -> "stmt.getSignature() is null for " + stmt);
    final Iterator iterator;
    Iterator stmtResultSet = stmt.getResultSet();
    if (stmtResultSet == null) {
      final Iterable iterable =
          _createIterable(h, signature, null, null);
      iterator = iterable.iterator();
      stmt.setResultSet(iterator);
    } else {
      iterator = stmtResultSet;
    }
    final List rows =
        MetaImpl.collect(signature.cursorFactory,
            LimitIterator.of(iterator, fetchMaxRowCount),
            new ArrayList>());
    boolean done = fetchMaxRowCount == 0 || rows.size() < fetchMaxRowCount;
    @SuppressWarnings("unchecked") List rows1 = (List) rows;
    return new Meta.Frame(offset, done, rows1);
  }

  @SuppressWarnings("deprecation")
  @Override public ExecuteResult execute(StatementHandle h,
      List parameterValues, long maxRowCount)
      throws NoSuchStatementException {
    return execute(h, parameterValues, Ints.saturatedCast(maxRowCount));
  }

  @Override public ExecuteResult execute(StatementHandle h,
      List parameterValues, int maxRowsInFirstFrame)
      throws NoSuchStatementException {
    final CalciteConnectionImpl calciteConnection = getConnection();
    CalciteServerStatement stmt = calciteConnection.server.getStatement(h);
    final Signature signature = requireNonNull(stmt.getSignature(),
        () -> "stmt.getSignature() is null for " + stmt);

    MetaResultSet metaResultSet;
    if (signature.statementType.canUpdate()) {
      final Iterable iterable =
          _createIterable(h, signature, parameterValues, null);
      final Iterator iterator = iterable.iterator();
      stmt.setResultSet(iterator);
      metaResultSet = MetaResultSet.count(h.connectionId, h.id,
          ((Number) iterator.next()).intValue());
    } else {
      // Don't populate the first frame.
      // It's not worth saving a round-trip, since we're local.
      final Meta.Frame frame =
          new Meta.Frame(0, false, Collections.emptyList());
      metaResultSet =
          MetaResultSet.create(h.connectionId, h.id, false, signature, frame);
    }

    return new ExecuteResult(ImmutableList.of(metaResultSet));
  }

  @Override public ExecuteBatchResult executeBatch(StatementHandle h,
      List> parameterValueLists) throws NoSuchStatementException {
    final List updateCounts = new ArrayList<>();
    for (List parameterValueList : parameterValueLists) {
      ExecuteResult executeResult = execute(h, parameterValueList, -1);
      final long updateCount =
          executeResult.resultSets.size() == 1
              ? executeResult.resultSets.get(0).updateCount
              : -1L;
      updateCounts.add(updateCount);
    }
    return new ExecuteBatchResult(Longs.toArray(updateCounts));
  }

  @Override public ExecuteBatchResult prepareAndExecuteBatch(
      final StatementHandle h,
      List sqlCommands) throws NoSuchStatementException {
    final CalciteConnectionImpl calciteConnection = getConnection();
    final CalciteServerStatement statement =
        calciteConnection.server.getStatement(h);
    final List updateCounts = new ArrayList<>();
    final Meta.PrepareCallback callback =
        new Meta.PrepareCallback() {
          long updateCount;
          @Nullable Signature signature;

          @Override public Object getMonitor() {
            return statement;
          }

          @Override public void clear() throws SQLException {}

          @Override public void assign(Meta.Signature signature, Meta.@Nullable Frame firstFrame,
              long updateCount) throws SQLException {
            this.signature = signature;
            this.updateCount = updateCount;
          }

          @Override public void execute() throws SQLException {
            Signature signature = requireNonNull(this.signature, "signature");
            if (signature.statementType.canUpdate()) {
              final Iterable iterable =
                  _createIterable(h, signature, ImmutableList.of(),
                      null);
              final Iterator iterator = iterable.iterator();
              updateCount = ((Number) iterator.next()).longValue();
            }
            updateCounts.add(updateCount);
          }
        };
    for (String sqlCommand : sqlCommands) {
      Util.discard(prepareAndExecute(h, sqlCommand, -1L, -1, callback));
    }
    return new ExecuteBatchResult(Longs.toArray(updateCounts));
  }

  /** A trojan-horse method, subject to change without notice. */
  @VisibleForTesting
  public static DataContext createDataContext(CalciteConnection connection) {
    return ((CalciteConnectionImpl) connection)
        .createDataContext(ImmutableMap.of(),
            CalciteSchema.from(connection.getRootSchema()));
  }

  /** A trojan-horse method, subject to change without notice. */
  @VisibleForTesting
  public static CalciteConnection connect(CalciteSchema schema,
      @Nullable JavaTypeFactory typeFactory) {
    return DRIVER.connect(schema, typeFactory);
  }

  @Override public boolean syncResults(StatementHandle h, QueryState state, long offset)
      throws NoSuchStatementException {
    // Doesn't have application in Calcite itself.
    throw new UnsupportedOperationException();
  }

  @Override public void commit(ConnectionHandle ch) {
    throw new UnsupportedOperationException();
  }

  @Override public void rollback(ConnectionHandle ch) {
    throw new UnsupportedOperationException();
  }

  /** Metadata describing a Calcite table. */
  private static class CalciteMetaTable extends MetaTable {
    private final Table calciteTable;

    CalciteMetaTable(Table calciteTable, String tableCat,
        String tableSchem, String tableName) {
      super(tableCat, tableSchem, tableName,
          calciteTable.getJdbcTableType().jdbcName);
      this.calciteTable = requireNonNull(calciteTable, "calciteTable");
    }
  }

  /** Metadata describing a Calcite schema. */
  private static class CalciteMetaSchema extends MetaSchema {
    private final CalciteSchema calciteSchema;

    CalciteMetaSchema(CalciteSchema calciteSchema,
        String tableCatalog, String tableSchem) {
      super(tableCatalog, tableSchem);
      this.calciteSchema = calciteSchema;
    }
  }

  /** Table whose contents are metadata.
   *
   * @param  element type */
  abstract static class MetadataTable extends AbstractQueryableTable {
    MetadataTable(Class clazz) {
      super(clazz);
    }

    @Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      return ((JavaTypeFactory) typeFactory).createType(elementType);
    }

    @Override public Schema.TableType getJdbcTableType() {
      return Schema.TableType.SYSTEM_TABLE;
    }

    @SuppressWarnings("unchecked")
    @Override public Class getElementType() {
      return (Class) elementType;
    }

    protected abstract Enumerator enumerator(CalciteMetaImpl connection);

    @Override public  Queryable asQueryable(QueryProvider queryProvider,
        SchemaPlus schema, String tableName) {
      return new AbstractTableQueryable(queryProvider, schema, this,
          tableName) {
        @SuppressWarnings("unchecked")
        @Override public Enumerator enumerator() {
          return (Enumerator) MetadataTable.this.enumerator(
              ((CalciteConnectionImpl) queryProvider).meta());
        }
      };
    }
  }

  /** Iterator that returns at most {@code limit} rows from an underlying
   * {@link Iterator}.
   *
   * @param  element type */
  private static class LimitIterator implements Iterator {
    private final Iterator iterator;
    private final long limit;
    int i = 0;

    private LimitIterator(Iterator iterator, long limit) {
      this.iterator = iterator;
      this.limit = limit;
    }

    static  Iterator of(Iterator iterator, long limit) {
      if (limit <= 0) {
        return iterator;
      }
      return new LimitIterator<>(iterator, limit);
    }

    @Override public boolean hasNext() {
      return iterator.hasNext() && i < limit;
    }

    @Override public E next() {
      ++i;
      return iterator.next();
    }

    @Override public void remove() {
      throw new UnsupportedOperationException();
    }
  }
}