Example #1
0
  private OperatorFactory compileFilterProject(
      Expression filter, Expression projection, ExpressionCompiler compiler) {
    filter = ExpressionTreeRewriter.rewriteWith(new SymbolToInputRewriter(INPUT_MAPPING), filter);
    projection =
        ExpressionTreeRewriter.rewriteWith(new SymbolToInputRewriter(INPUT_MAPPING), projection);

    IdentityHashMap<Expression, Type> expressionTypes =
        getExpressionTypesFromInput(
            TEST_SESSION, metadata, SQL_PARSER, INPUT_TYPES, ImmutableList.of(filter, projection));

    try {
      List<RowExpression> projections =
          ImmutableList.of(toRowExpression(projection, expressionTypes));
      PageProcessor processor =
          compiler.compilePageProcessor(toRowExpression(filter, expressionTypes), projections);

      return new FilterAndProjectOperator.FilterAndProjectOperatorFactory(
          0, processor, ImmutableList.of(expressionTypes.get(projection)));
    } catch (Throwable e) {
      if (e instanceof UncheckedExecutionException) {
        e = e.getCause();
      }
      throw new RuntimeException("Error compiling " + projection + ": " + e.getMessage(), e);
    }
  }
Example #2
0
  private OperatorFactory compileFilterWithNoInputColumns(
      Expression filter, ExpressionCompiler compiler) {
    filter =
        ExpressionTreeRewriter.rewriteWith(
            new SymbolToInputRewriter(ImmutableMap.<Symbol, Integer>of()), filter);

    IdentityHashMap<Expression, Type> expressionTypes =
        getExpressionTypesFromInput(
            TEST_SESSION, metadata, SQL_PARSER, INPUT_TYPES, ImmutableList.of(filter));

    try {
      PageProcessor processor =
          compiler.compilePageProcessor(
              toRowExpression(filter, expressionTypes), ImmutableList.of());

      return new FilterAndProjectOperator.FilterAndProjectOperatorFactory(
          0, processor, ImmutableList.<Type>of());
    } catch (Throwable e) {
      if (e instanceof UncheckedExecutionException) {
        e = e.getCause();
      }
      throw new RuntimeException("Error compiling " + filter + ": " + e.getMessage(), e);
    }
  }
    @Setup
    public void setup() {
      MetadataManager metadata = MetadataManager.createTestMetadataManager();
      ExpressionCompiler compiler = new ExpressionCompiler(metadata);

      List<String> keys;
      switch (mapSize) {
        case 1:
          keys = ImmutableList.of("do_not_use");
          break;
        case 13:
          keys =
              ImmutableList.of(
                  "is_inverted",
                  "device_model",
                  "country",
                  "carrier_id",
                  "network_type",
                  "os_version",
                  "device_brand",
                  "device_type",
                  "interface",
                  "device_os",
                  "app_version",
                  "device_type_class",
                  "browser");
          break;
        default:
          throw new UnsupportedOperationException();
      }
      verify(keys.size() == mapSize);

      MapType mapType;
      Block valueBlock;
      switch (name) {
        case "fix-width":
          mapType = new MapType(createUnboundedVarcharType(), DOUBLE);
          valueBlock = createFixWidthValueBlock(POSITIONS, mapSize);
          break;
        case "var-width":
          mapType = new MapType(createUnboundedVarcharType(), createUnboundedVarcharType());
          valueBlock = createVarWidthValueBlock(POSITIONS, mapSize);
          break;
        case "dictionary":
          mapType = new MapType(createUnboundedVarcharType(), createUnboundedVarcharType());
          valueBlock = createDictionaryValueBlock(POSITIONS, mapSize);
          break;
        default:
          throw new UnsupportedOperationException();
      }

      Block keyBlock = createKeyBlock(POSITIONS, keys);
      Block block = createMapBlock(POSITIONS, keyBlock, valueBlock);

      ImmutableList.Builder<RowExpression> projectionsBuilder = ImmutableList.builder();

      Signature signature =
          new Signature(
              "$operator$" + SUBSCRIPT.name(),
              FunctionKind.SCALAR,
              mapType.getValueType().getTypeSignature(),
              mapType.getTypeSignature(),
              mapType.getKeyType().getTypeSignature());
      for (int i = 0; i < mapSize; i++) {
        projectionsBuilder.add(
            new CallExpression(
                signature,
                mapType.getValueType(),
                ImmutableList.of(
                    new InputReferenceExpression(0, mapType),
                    new ConstantExpression(utf8Slice(keys.get(i)), createUnboundedVarcharType()))));
      }

      ImmutableList<RowExpression> projections = projectionsBuilder.build();
      pageProcessor =
          compiler
              .compilePageProcessor(new ConstantExpression(true, BooleanType.BOOLEAN), projections)
              .get();
      pageBuilder =
          new PageBuilder(
              projections.stream().map(RowExpression::getType).collect(Collectors.toList()));
      page = new Page(block);
    }