org.apache.spark.sql.catalyst.expressions.Attribute Java Examples

The following examples show how to use org.apache.spark.sql.catalyst.expressions.Attribute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuicksqlServerMeta.java    From Quicksql with MIT License 6 votes vote down vote up
public QueryResult getSparkQueryResult(Entry<List<Attribute>, List<GenericRowWithSchema>> sparkData)
    throws Exception {
    if (sparkData == null) {
        return new QueryResult(new ArrayList<>(), new ArrayList<>());
    }
    if (CollectionUtils.isEmpty(sparkData.getKey())) {
        throw new SparkException("collect data error");
    }
    List<Attribute> attributes = sparkData.getKey();
    List<GenericRowWithSchema> value = sparkData.getValue();
    List<Object> data = new ArrayList<>();
    List<ColumnMetaData> meta = new ArrayList<>();
    value.stream().forEach(column -> {
        data.add(column.values());
    });
    for (int index = 0; index < sparkData.getKey().size(); index++) {
        Attribute attribute = sparkData.getKey().get(index);
        ScalarType columnType = getColumnType(attribute.dataType());
        meta.add(new ColumnMetaData(index, false, true, false, false,
            attribute.nullable() ? 1 : 0, true, -1, attribute.name(), attribute.name(), null, -1, -1, null, null,
            columnType, true, false, false, columnType.columnClassName()));
    }
    return new QueryResult(meta, data);
}
 
Example #2
Source File: SparkBenchmarkUtil.java    From iceberg with Apache License 2.0 6 votes vote down vote up
public static UnsafeProjection projection(Schema expectedSchema, Schema actualSchema) {
  StructType struct = SparkSchemaUtil.convert(actualSchema);

  List<AttributeReference> refs = JavaConverters.seqAsJavaListConverter(struct.toAttributes()).asJava();
  List<Attribute> attrs = Lists.newArrayListWithExpectedSize(struct.fields().length);
  List<Expression> exprs = Lists.newArrayListWithExpectedSize(struct.fields().length);

  for (AttributeReference ref : refs) {
    attrs.add(ref.toAttribute());
  }

  for (Types.NestedField field : expectedSchema.columns()) {
    int indexInIterSchema = struct.fieldIndex(field.name());
    exprs.add(refs.get(indexInIterSchema));
  }

  return UnsafeProjection.create(
      JavaConverters.asScalaBufferConverter(exprs).asScala().toSeq(),
      JavaConverters.asScalaBufferConverter(attrs).asScala().toSeq());
}
 
Example #3
Source File: RowDataReader.java    From iceberg with Apache License 2.0 6 votes vote down vote up
private static UnsafeProjection projection(Schema finalSchema, Schema readSchema) {
  StructType struct = SparkSchemaUtil.convert(readSchema);

  List<AttributeReference> refs = JavaConverters.seqAsJavaListConverter(struct.toAttributes()).asJava();
  List<Attribute> attrs = Lists.newArrayListWithExpectedSize(struct.fields().length);
  List<org.apache.spark.sql.catalyst.expressions.Expression> exprs =
      Lists.newArrayListWithExpectedSize(struct.fields().length);

  for (AttributeReference ref : refs) {
    attrs.add(ref.toAttribute());
  }

  for (Types.NestedField field : finalSchema.columns()) {
    int indexInReadSchema = struct.fieldIndex(field.name());
    exprs.add(refs.get(indexInReadSchema));
  }

  return UnsafeProjection.create(
      JavaConverters.asScalaBufferConverter(exprs).asScala().toSeq(),
      JavaConverters.asScalaBufferConverter(attrs).asScala().toSeq());
}
 
Example #4
Source File: Reader.java    From iceberg with Apache License 2.0 6 votes vote down vote up
private static UnsafeProjection projection(Schema finalSchema, Schema readSchema) {
  StructType struct = convert(readSchema);

  List<AttributeReference> refs = seqAsJavaListConverter(struct.toAttributes()).asJava();
  List<Attribute> attrs = Lists.newArrayListWithExpectedSize(struct.fields().length);
  List<org.apache.spark.sql.catalyst.expressions.Expression> exprs =
      Lists.newArrayListWithExpectedSize(struct.fields().length);

  for (AttributeReference ref : refs) {
    attrs.add(ref.toAttribute());
  }

  for (Types.NestedField field : finalSchema.columns()) {
    int indexInReadSchema = struct.fieldIndex(field.name());
    exprs.add(refs.get(indexInReadSchema));
  }

  return UnsafeProjection.create(
      asScalaBufferConverter(exprs).asScala().toSeq(),
      asScalaBufferConverter(attrs).asScala().toSeq());
}
 
Example #5
Source File: QuicksqlServerMeta.java    From Quicksql with MIT License 5 votes vote down vote up
private QuicksqlServerResultSet getResultSet(StatementHandle h, SqlRunner runner, String sql, int maxResNum,
    Object collect) {
    if (collect instanceof ResultSet) {
        return getJDBCResultSet(h, collect, maxResNum);
    } else if (collect instanceof Map.Entry) {
        try {
            Entry<List<Attribute>, List<GenericRowWithSchema>> sparkData = (Entry<List<Attribute>, List<GenericRowWithSchema>>) collect;
            return getResultSet(h, sql, maxResNum, getSparkQueryResult(sparkData));
        } catch (Exception e) {
            throw new RuntimeException("result type error " + e.getMessage());
        }
    } else {
        throw new RuntimeException("not matching result type");
    }
}
 
Example #6
Source File: SparkExpressions.java    From iceberg with Apache License 2.0 5 votes vote down vote up
private static com.netflix.iceberg.expressions.Expression convertIn(Expression expr,
                                                                    Collection<Object> values) {
  if (expr instanceof Attribute) {
    Attribute attr = (Attribute) expr;
    com.netflix.iceberg.expressions.Expression converted = alwaysFalse();
    for (Object item : values) {
      converted = or(converted, equal(attr.name(), item));
    }
    return converted;
  }

  return null;
}
 
Example #7
Source File: QuicksqlServerMeta.java    From Quicksql with MIT License 4 votes vote down vote up
private ExecuteResult getExecuteResultSet(StatementHandle h, QuicksqlConnectionImpl connection, String sql) {
    MetaResultSet resultSet = null;
    String responseUrl = "";
    System.out.println("sql:" + sql);
    try {
        if (sql.toLowerCase().startsWith("explain")) {
            String logicalPlanView = new SqlLogicalPlanView().getLogicalPlanView(sql.replaceAll("explain ",""));
            resultSet = getResultSet(h, sql, 1, getExplainResult(logicalPlanView));
            return new ExecuteResult(Collections.singletonList(resultSet));
        }

        int maxResNum = Integer
            .parseInt(StringUtils.defaultIfBlank(connection.getInfoByName("acceptedResultsNum"), "100000"));
        responseUrl = connection.getInfoByName("responseUrl");

        SqlRunner runner = SqlRunner.builder()
            .setTransformRunner(RunnerType.value(connection.getInfoByName("runner")))
            .setSchemaPath(StringUtils.isNotBlank(connection.getInfoByName("schemaPath")) ? "inline:" + connection
                .getInfoByName("schemaPath") : SqlUtil.getSchemaPath(SqlUtil.parseTableName(sql).tableNames))
            .setAppName(StringUtils.defaultIfBlank(connection.getInfoByName("appName"), ""))
            .setAcceptedResultsNum(maxResNum)
            .ok();
        if (sql.contains("HDFS")) {
            insertResult(sql, runner, connection);
            resultSet = getResultSet(h, sql, 0, new QueryResult(new ArrayList<>(), new ArrayList<>()));
        } else {
            RunnerType runnerType = RunnerType.value(connection.getInfoByName("runner"));
            Object collect = runner.sql(sql).collect();
            switch (runnerType) {
                case JDBC:
                    resultSet = getJDBCResultSet(h, collect, maxResNum);
                    break;
                case SPARK:
                    Entry<List<Attribute>, List<GenericRowWithSchema>> sparkData = (Entry<List<Attribute>, List<GenericRowWithSchema>>) collect;
                    resultSet = getResultSet(h, sql, maxResNum, getSparkQueryResult(sparkData));
                    break;
                case FLINK:
                    Entry<TableSchema, List<Row>> flinkData = (Entry<TableSchema, List<Row>>) collect;
                    resultSet = getResultSet(h, sql, maxResNum, getFlinkQueryResult(flinkData));
                    break;
                case DEFAULT:
                    resultSet = getResultSet(h, runner, sql, maxResNum, collect);
                    break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        setHttpResponse(responseUrl, 0, "Quicksql error :" + e.getCause());
        throw new RuntimeException(e);
    }
    return new ExecuteResult(Collections.singletonList(resultSet));
}
 
Example #8
Source File: SparkSqlInterpreter.java    From Explorer with Apache License 2.0 4 votes vote down vote up
@Override
public InterpreterResult interpret(String st) {

    SQLContext sqlc = getSparkInterpreter().getSQLContext();
    SparkContext sc = sqlc.sparkContext();
    sc.setJobGroup(jobGroup, "Notebook", false);
    DataFrame dataFrame;
    Row[] rows = null;
    try {
        dataFrame = sqlc.sql(st);
        rows = dataFrame.take(maxResult + 1);
    } catch (Exception e) {
        logger.error("Error", e);
        sc.clearJobGroup();
        return new InterpreterResult(Code.ERROR, e.getMessage());
    }

    String msg = null;
    // get field names
    List<Attribute> columns = scala.collection.JavaConverters.asJavaListConverter(
            dataFrame.queryExecution().analyzed().output()).asJava();
    for (Attribute col : columns) {
        if (msg == null) {
            msg = col.name();
        } else {
            msg += "\t" + col.name();
        }
    }
    msg += "\n";

    // ArrayType, BinaryType, BooleanType, ByteType, DecimalType, DoubleType, DynamicType, FloatType, FractionalType, IntegerType, IntegralType, LongType, MapType, NativeType, NullType, NumericType, ShortType, StringType, StructType

    for (int r = 0; r < maxResult && r < rows.length; r++) {
        Row row = rows[r];

        for (int i = 0; i < columns.size(); i++) {
            if (!row.isNullAt(i)) {
                msg += row.apply(i).toString();
            } else {
                msg += "null";
            }
            if (i != columns.size() - 1) {
                msg += "\t";
            }
        }
        msg += "\n";
    }

    if (rows.length > maxResult) {
        msg += "\n<font color=red>Results are limited by " + maxResult + ".</font>";
    }
    InterpreterResult rett = new InterpreterResult(Code.SUCCESS, "%table " + msg);
    sc.clearJobGroup();
    return rett;
}