Пример #1
0
 public RexNode parameter(ParameterExpression param) {
   int i = parameterList.indexOf(param);
   if (i >= 0) {
     return values.get(i);
   }
   throw new RuntimeException("unknown parameter " + param);
 }
Пример #2
0
 public List<RexNode> toRexList(BlockExpression expression) {
   final List<Expression> simpleList = simpleList(expression);
   final List<RexNode> list = new ArrayList<RexNode>();
   for (Expression expression1 : simpleList) {
     list.add(toRex(expression1));
   }
   return list;
 }
Пример #3
0
 public List<SqlOperator> lookupOperatorOverloads(
     SqlIdentifier opName, SqlFunctionCategory category, SqlSyntax syntax) {
   if (syntax != SqlSyntax.Function) {
     return Collections.emptyList();
   }
   // FIXME: ignoring prefix of opName
   String name = opName.names[opName.names.length - 1];
   List<TableFunction> tableFunctions = rootSchema.getTableFunctions(name);
   if (tableFunctions.isEmpty()) {
     return Collections.emptyList();
   }
   return toOps(name, tableFunctions);
 }
Пример #4
0
 /** Returns the path of an object in this schema. */
 public List<String> path(String name) {
   final List<String> list = new ArrayList<String>();
   if (name != null) {
     list.add(name);
   }
   for (OptiqSchema s = this; s != null; s = s.parent) {
     if (s.parent != null || !s.name.equals("")) {
       // Omit the root schema's name from the path if it's the empty string,
       // which it usually is.
       list.add(s.name);
     }
   }
   return ImmutableList.copyOf(Lists.reverse(list));
 }
Пример #5
0
 private SqlOperator toOp(SqlIdentifier name, Function function) {
   List<RelDataType> argTypes = new ArrayList<RelDataType>();
   List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>();
   for (FunctionParameter o : function.getParameters()) {
     final RelDataType type = o.getType(typeFactory);
     argTypes.add(type);
     typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
   }
   final RelDataType returnType;
   if (function instanceof ScalarFunction) {
     return new SqlUserDefinedFunction(
         name,
         ReturnTypes.explicit(Schemas.proto((ScalarFunction) function)),
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         toSql(argTypes),
         function);
   } else if (function instanceof AggregateFunction) {
     returnType = ((AggregateFunction) function).getReturnType(typeFactory);
     return new SqlUserDefinedAggFunction(
         name,
         ReturnTypes.explicit(returnType),
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         (AggregateFunction) function);
   } else if (function instanceof TableMacro) {
     return new SqlUserDefinedTableMacro(
         name,
         ReturnTypes.CURSOR,
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         (TableMacro) function);
   } else if (function instanceof TableFunction) {
     return new SqlUserDefinedTableFunction(
         name,
         ReturnTypes.CURSOR,
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         toSql(argTypes),
         (TableFunction) function);
   } else {
     throw new AssertionError("unknown function type " + function);
   }
 }
Пример #6
0
 public Object freeze(ColumnLoader.ValueSet valueSet) {
   final int chunksPerWord = 64 / bitCount;
   final List<Comparable> valueList = valueSet.values;
   final int valueCount = valueList.size();
   final int wordCount = (valueCount + (chunksPerWord - 1)) / chunksPerWord;
   final int remainingChunkCount = valueCount % chunksPerWord;
   final long[] longs = new long[wordCount];
   final int n = valueCount / chunksPerWord;
   int i;
   int k = 0;
   if (valueCount > 0 && valueList.get(0) instanceof Boolean) {
     @SuppressWarnings("unchecked")
     final List<Boolean> booleans = (List) valueSet.values;
     for (i = 0; i < n; i++) {
       long v = 0;
       for (int j = 0; j < chunksPerWord; j++) {
         v |= (booleans.get(k++) ? (1 << (bitCount * j)) : 0);
       }
       longs[i] = v;
     }
     if (remainingChunkCount > 0) {
       long v = 0;
       for (int j = 0; j < remainingChunkCount; j++) {
         v |= (booleans.get(k++) ? (1 << (bitCount * j)) : 0);
       }
       longs[i] = v;
     }
   } else {
     @SuppressWarnings("unchecked")
     final List<Number> numbers = (List) valueSet.values;
     for (i = 0; i < n; i++) {
       long v = 0;
       for (int j = 0; j < chunksPerWord; j++) {
         v |= (numbers.get(k++).longValue() << (bitCount * j));
       }
       longs[i] = v;
     }
     if (remainingChunkCount > 0) {
       long v = 0;
       for (int j = 0; j < remainingChunkCount; j++) {
         v |= (numbers.get(k++).longValue() << (bitCount * j));
       }
       longs[i] = v;
     }
   }
   return longs;
 }
Пример #7
0
  public List<SqlMoniker> getAllSchemaObjectNames(List<String> names) {
    final OptiqSchema schema = getSchema(names);
    if (schema == null) {
      return ImmutableList.of();
    }
    final List<SqlMoniker> result = new ArrayList<SqlMoniker>();
    final Map<String, OptiqSchema> schemaMap = schema.getSubSchemaMap();

    for (String subSchema : schemaMap.keySet()) {
      result.add(new SqlMonikerImpl(schema.path(subSchema), SqlMonikerType.SCHEMA));
    }

    for (String table : schema.getTableNames()) {
      result.add(new SqlMonikerImpl(schema.path(table), SqlMonikerType.TABLE));
    }

    final NavigableSet<String> functions = schema.getFunctionNames();
    for (String function : functions) { // views are here as well
      result.add(new SqlMonikerImpl(schema.path(function), SqlMonikerType.FUNCTION));
    }
    return result;
  }
Пример #8
0
 private Collection<Function> getFunctionsFrom(List<String> names) {
   final List<Function> functions2 = Lists.newArrayList();
   final List<? extends List<String>> schemaNameList;
   if (names.size() > 1) {
     // If name is qualified, ignore path.
     schemaNameList = ImmutableList.of(ImmutableList.<String>of());
   } else {
     OptiqSchema schema = getSchema(defaultSchema);
     if (schema == null) {
       schemaNameList = ImmutableList.of();
     } else {
       schemaNameList = schema.getPath();
     }
   }
   for (List<String> schemaNames : schemaNameList) {
     OptiqSchema schema = getSchema(Iterables.concat(schemaNames, Util.skipLast(names)));
     if (schema != null) {
       final String name = Util.last(names);
       functions2.addAll(schema.getFunctions(name, true));
     }
   }
   return functions2;
 }
Пример #9
0
 private SqlOperator toOp(String name, TableFunction fun) {
   List<RelDataType> argTypes = new ArrayList<RelDataType>();
   List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>();
   Parameter p;
   for (net.hydromatic.optiq.Parameter o :
       (List<net.hydromatic.optiq.Parameter>) fun.getParameters()) {
     argTypes.add(o.getType());
     typeFamilies.add(SqlTypeFamily.ANY);
   }
   return new SqlFunction(
       name,
       SqlKind.OTHER_FUNCTION,
       new ExplicitReturnTypeInference(typeFactory.createType(fun.getElementType())),
       new ExplicitOperandTypeInference(argTypes.toArray(new RelDataType[argTypes.size()])),
       new FamilyOperandTypeChecker(
           typeFamilies.toArray(new SqlTypeFamily[typeFamilies.size()])),
       null);
 }
Пример #10
0
 public void lookupOperatorOverloads(
     final SqlIdentifier opName,
     SqlFunctionCategory category,
     SqlSyntax syntax,
     List<SqlOperator> operatorList) {
   if (syntax != SqlSyntax.FUNCTION) {
     return;
   }
   final Collection<Function> functions = getFunctionsFrom(opName.names);
   if (functions.isEmpty()) {
     return;
   }
   operatorList.addAll(
       Collections2.transform(
           functions,
           new com.google.common.base.Function<Function, SqlOperator>() {
             public SqlOperator apply(Function function) {
               return toOp(opName, function);
             }
           }));
 }
Пример #11
0
 public Statistic getStatistic() {
   return Statistics.of(list.size(), Collections.<BitSet>emptyList());
 }
Пример #12
0
  <T> PrepareResult<T> prepare_(
      Context context, String sql, Queryable<T> queryable, Type elementType) {
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    OptiqCatalogReader catalogReader = new OptiqCatalogReader(context.getRootSchema(), typeFactory);
    final OptiqPreparingStmt preparingStmt =
        new OptiqPreparingStmt(catalogReader, typeFactory, context.getRootSchema());
    preparingStmt.setResultCallingConvention(CallingConvention.ENUMERABLE);

    final RelDataType x;
    final PreparedResult preparedResult;
    if (sql != null) {
      assert queryable == null;
      SqlParser parser = new SqlParser(sql);
      SqlNode sqlNode;
      try {
        sqlNode = parser.parseQuery();
      } catch (SqlParseException e) {
        throw new RuntimeException("parse failed", e);
      }
      final Schema rootSchema = context.getRootSchema();
      SqlValidator validator =
          new SqlValidatorImpl(
              new ChainedSqlOperatorTable(
                  Arrays.<SqlOperatorTable>asList(
                      SqlStdOperatorTable.instance(),
                      new MySqlOperatorTable(rootSchema, typeFactory))),
              catalogReader,
              typeFactory,
              SqlConformance.Default) {};
      preparedResult = preparingStmt.prepareSql(sqlNode, Object.class, validator, true);
      x = validator.getValidatedNodeType(sqlNode);
    } else {
      assert queryable != null;
      x = context.getTypeFactory().createType(elementType);
      preparedResult = preparingStmt.prepareQueryable(queryable, x);
    }

    // TODO: parameters
    final List<Parameter> parameters = Collections.emptyList();
    // TODO: column meta data
    final List<ColumnMetaData> columns = new ArrayList<ColumnMetaData>();
    RelDataType jdbcType = makeStruct(typeFactory, x);
    for (RelDataTypeField field : jdbcType.getFields()) {
      RelDataType type = field.getType();
      SqlTypeName sqlTypeName = type.getSqlTypeName();
      columns.add(
          new ColumnMetaData(
              columns.size(),
              false,
              true,
              false,
              false,
              type.isNullable() ? 1 : 0,
              true,
              0,
              field.getName(),
              field.getName(),
              null,
              sqlTypeName.allowsPrec() && false ? type.getPrecision() : -1,
              sqlTypeName.allowsScale() ? type.getScale() : -1,
              null,
              null,
              sqlTypeName.getJdbcOrdinal(),
              sqlTypeName.getName(),
              true,
              false,
              false,
              null));
    }
    return new PrepareResult<T>(sql, parameters, columns, (Enumerable<T>) preparedResult.execute());
  }
Пример #13
0
 public void addStatement(OptiqServerStatement statement) {
   statementList.add(statement);
 }
Пример #14
0
 public void removeStatement(OptiqServerStatement optiqServerStatement) {
   statementList.add(optiqServerStatement);
 }