Search in sources :

Example 1 with SqlSchema

use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.

the class AvroTypeFactoryImpl method convertSchema.

private SqlSchema convertSchema(List<Schema.Field> fields, boolean isTopLevelField) {
    SqlSchemaBuilder schemaBuilder = SqlSchemaBuilder.builder();
    for (Schema.Field field : fields) {
        SqlFieldSchema fieldSchema = convertField(field.schema(), false, isOptional(field, isTopLevelField));
        schemaBuilder.addField(field.name(), fieldSchema);
    }
    return schemaBuilder.build();
}
Also used : SqlFieldSchema(org.apache.samza.sql.schema.SqlFieldSchema) SqlFieldSchema(org.apache.samza.sql.schema.SqlFieldSchema) Schema(org.apache.avro.Schema) SqlSchema(org.apache.samza.sql.schema.SqlSchema) SqlSchemaBuilder(org.apache.samza.sql.schema.SqlSchemaBuilder)

Example 2 with SqlSchema

use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.

the class SamzaExecutor method getTableSchema.

@Override
public SqlSchema getTableSchema(ExecutionContext context, String tableName) throws ExecutorException {
    /*
     *  currently Shell works only for systems that has Avro schemas
     */
    int execId = execIdSeq.incrementAndGet();
    Map<String, String> staticConfigs = fetchSamzaSqlConfig(execId);
    Config samzaSqlConfig = new MapConfig(staticConfigs);
    SqlSchema sqlSchema;
    try {
        SqlIOResolver ioResolver = SamzaSqlApplicationConfig.createIOResolver(samzaSqlConfig);
        SqlIOConfig sourceInfo = ioResolver.fetchSourceInfo(tableName);
        RelSchemaProvider schemaProvider = SamzaSqlApplicationConfig.initializePlugin("RelSchemaProvider", sourceInfo.getRelSchemaProviderName(), samzaSqlConfig, SamzaSqlApplicationConfig.CFG_FMT_REL_SCHEMA_PROVIDER_DOMAIN, (o, c) -> ((RelSchemaProviderFactory) o).create(sourceInfo.getSystemStream(), c));
        sqlSchema = schemaProvider.getSqlSchema();
    } catch (SamzaException ex) {
        throw new ExecutorException(ex);
    }
    return sqlSchema;
}
Also used : SqlIOConfig(org.apache.samza.sql.interfaces.SqlIOConfig) ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException) SqlSchema(org.apache.samza.sql.schema.SqlSchema) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) SqlIOConfig(org.apache.samza.sql.interfaces.SqlIOConfig) SqlIOResolver(org.apache.samza.sql.interfaces.SqlIOResolver) RelSchemaProvider(org.apache.samza.sql.interfaces.RelSchemaProvider) SamzaException(org.apache.samza.SamzaException)

Example 3 with SqlSchema

use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.

the class SamzaExecutorTest method testGenerateResultSchema.

// Generate result schema needs to be fixed. SAMZA-2079
@Ignore
@Test
public void testGenerateResultSchema() {
    prepareEnvironmentVariable();
    Map<String, String> mapConf = mExecutor.fetchSamzaSqlConfig(1);
    SqlSchema ts = mExecutor.generateResultSchema(new MapConfig(mapConf));
    List<SqlSchema.SqlField> fields = ts.getFields();
    Assert.assertEquals("__key__", fields.get(0).getFieldName());
    Assert.assertEquals("Name", fields.get(1).getFieldName());
    Assert.assertEquals("NewCompany", fields.get(2).getFieldName());
    Assert.assertEquals("OldCompany", fields.get(3).getFieldName());
    Assert.assertEquals("ProfileChangeTimestamp", fields.get(4).getFieldName());
    Assert.assertEquals("ANY", fields.get(0).getFieldSchema().getFieldType().toString());
    Assert.assertEquals("VARCHAR", fields.get(1).getFieldSchema().getFieldType().toString());
    Assert.assertEquals("VARCHAR", fields.get(2).getFieldSchema().getFieldType().toString());
    Assert.assertEquals("VARCHAR", fields.get(3).getFieldSchema().getFieldType().toString());
    Assert.assertEquals("BIGINT", fields.get(4).getFieldSchema().getFieldType().toString());
}
Also used : SqlSchema(org.apache.samza.sql.schema.SqlSchema) MapConfig(org.apache.samza.config.MapConfig) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with SqlSchema

use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.

the class CliCommandHandler method formatSchema4Display.

/*
      Field    | Type
      -------------------------
      Field1   | Type 1
      Field2   | VARCHAR(STRING)
      Field... | VARCHAR(STRING)
      -------------------------
  */
private List<String> formatSchema4Display(SqlSchema schema) {
    final String headerField = "Field";
    final String headerType = "Type";
    final char seperator = '|';
    final char lineSep = '-';
    int terminalWidth = terminal.getWidth();
    // Two spaces * 2 plus one SEPERATOR
    if (terminalWidth < 2 + 2 + 1 + headerField.length() + headerType.length()) {
        return Collections.singletonList("Not enough room.");
    }
    // Find the best seperator position for least rows
    int seperatorPos = headerField.length() + 2;
    int minRowNeeded = Integer.MAX_VALUE;
    int longestLineCharNum = 0;
    int rowCount = schema.getFields().size();
    for (int j = seperatorPos; j < terminalWidth - headerType.length() - 2; ++j) {
        boolean fieldWrapped = false;
        int rowNeeded = 0;
        for (int i = 0; i < rowCount; ++i) {
            SqlSchema.SqlField field = schema.getFields().get(i);
            int fieldLen = field.getFieldName().length();
            int typeLen = field.getFieldSchema().getFieldType().toString().length();
            int fieldRowNeeded = CliUtil.ceilingDiv(fieldLen, j - 2);
            int typeRowNeeded = CliUtil.ceilingDiv(typeLen, terminalWidth - 1 - j - 2);
            rowNeeded += Math.max(fieldRowNeeded, typeRowNeeded);
            fieldWrapped |= fieldRowNeeded > 1;
            if (typeRowNeeded > 1) {
                longestLineCharNum = terminalWidth;
            } else {
                longestLineCharNum = Math.max(longestLineCharNum, j + typeLen + 2 + 1);
            }
        }
        if (rowNeeded < minRowNeeded) {
            minRowNeeded = rowNeeded;
            seperatorPos = j;
        }
        if (!fieldWrapped)
            break;
    }
    List<String> lines = new ArrayList<>(minRowNeeded + 4);
    // Header
    StringBuilder line = new StringBuilder(terminalWidth);
    line.append(CliConstants.SPACE);
    line.append(headerField);
    CliUtil.appendTo(line, seperatorPos - 1, CliConstants.SPACE);
    line.append(seperator);
    line.append(CliConstants.SPACE);
    line.append(headerType);
    lines.add(line.toString());
    line = new StringBuilder(terminalWidth);
    CliUtil.appendTo(line, longestLineCharNum - 1, lineSep);
    lines.add(line.toString());
    // Body
    AttributedStyle oddLineStyle = AttributedStyle.BOLD.foreground(AttributedStyle.BLUE);
    AttributedStyle evenLineStyle = AttributedStyle.BOLD.foreground(AttributedStyle.CYAN);
    final int fieldColSize = seperatorPos - 2;
    final int typeColSize = terminalWidth - seperatorPos - 1 - 2;
    for (int i = 0; i < rowCount; ++i) {
        SqlSchema.SqlField sqlField = schema.getFields().get(i);
        String field = sqlField.getFieldName();
        String type = getFieldDisplayValue(sqlField.getFieldSchema());
        int fieldLen = field.length();
        int typeLen = type.length();
        int fieldStartIdx = 0, typeStartIdx = 0;
        while (fieldStartIdx < fieldLen || typeStartIdx < typeLen) {
            line = new StringBuilder(terminalWidth);
            line.append(CliConstants.SPACE);
            int numToWrite = Math.min(fieldColSize, fieldLen - fieldStartIdx);
            if (numToWrite > 0) {
                line.append(field, fieldStartIdx, fieldStartIdx + numToWrite);
                fieldStartIdx += numToWrite;
            }
            CliUtil.appendTo(line, seperatorPos - 1, CliConstants.SPACE);
            line.append(seperator);
            line.append(CliConstants.SPACE);
            numToWrite = Math.min(typeColSize, typeLen - typeStartIdx);
            if (numToWrite > 0) {
                line.append(type, typeStartIdx, typeStartIdx + numToWrite);
                typeStartIdx += numToWrite;
            }
            if (i % 2 == 0) {
                AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(evenLineStyle);
                attrBuilder.append(line.toString());
                lines.add(attrBuilder.toAnsi());
            } else {
                AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(oddLineStyle);
                attrBuilder.append(line.toString());
                lines.add(attrBuilder.toAnsi());
            }
        }
    }
    // Footer
    line = new StringBuilder(terminalWidth);
    CliUtil.appendTo(line, longestLineCharNum - 1, lineSep);
    lines.add(line.toString());
    return lines;
}
Also used : AttributedStyle(org.jline.utils.AttributedStyle) SqlSchema(org.apache.samza.sql.schema.SqlSchema) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) ArrayList(java.util.ArrayList) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder)

Example 5 with SqlSchema

use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.

the class SamzaExecutor method generateResultSchema.

SqlSchema generateResultSchema(Config config) {
    SamzaSqlDslConverter converter = (SamzaSqlDslConverter) new SamzaSqlDslConverterFactory().create(config);
    RelRoot relRoot = converter.convertDsl("").iterator().next();
    List<String> colNames = new ArrayList<>();
    List<String> colTypeNames = new ArrayList<>();
    for (RelDataTypeField dataTypeField : relRoot.validatedRowType.getFieldList()) {
        colNames.add(dataTypeField.getName());
        colTypeNames.add(dataTypeField.getType().toString());
    }
    // in QueryResult class and executeQuery().
    return new SqlSchema(colNames, Collections.emptyList());
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SamzaSqlDslConverter(org.apache.samza.sql.dsl.SamzaSqlDslConverter) SqlSchema(org.apache.samza.sql.schema.SqlSchema) SamzaSqlDslConverterFactory(org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory) RelRoot(org.apache.calcite.rel.RelRoot)

Aggregations

SqlSchema (org.apache.samza.sql.schema.SqlSchema)13 SqlFieldSchema (org.apache.samza.sql.schema.SqlFieldSchema)4 SamzaException (org.apache.samza.SamzaException)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 RelRecordType (org.apache.calcite.rel.type.RelRecordType)2 ExecutorException (org.apache.samza.sql.client.exceptions.ExecutorException)2 Map (java.util.Map)1 Schema (org.apache.avro.Schema)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 RelRoot (org.apache.calcite.rel.RelRoot)1 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 MapConfig (org.apache.samza.config.MapConfig)1 ExecutionContext (org.apache.samza.sql.client.interfaces.ExecutionContext)1 SamzaSqlDslConverter (org.apache.samza.sql.dsl.SamzaSqlDslConverter)1 SamzaSqlDslConverterFactory (org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory)1 RelSchemaProvider (org.apache.samza.sql.interfaces.RelSchemaProvider)1 SqlIOConfig (org.apache.samza.sql.interfaces.SqlIOConfig)1