Java Code Examples for org.apache.avro.io.parsing.Symbol#Alternative

The following examples show how to use org.apache.avro.io.parsing.Symbol#Alternative . 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: GuidedJsonDecoder.java    From gcp-ingestion with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Find the index in the union of the current variant.
 *
 * <p>This method only supports a single nullable type. Having more than a single
 * type is invalid in this case and will cause the decoder to panic. This
 * behavior is by design, since BigQuery does not support variant types in
 * columns. It is also inefficient to match sub-documents against various
 * types, given the streaming interface and bias towards performance.
 *
 * <p>Variants of non-null types are invalid. We enforce this by ensuring there
 * are no more than 2 elements and that at least one of them is null if there
 * are 2. Unions are required to be non-empty.
 *
 * <li> Ok: [null], [type], [null, type]
 * <li> Bad: [type, type], [null, type, type]
 */
@Override
public int readIndex() throws IOException {
  parser.advance(Symbol.UNION);
  Symbol.Alternative top = (Symbol.Alternative) parser.popSymbol();

  int nullIndex = top.findLabel("null");
  int typeIndex = nullIndex == 0 ? 1 : 0;

  if ((nullIndex < 0 && top.size() == 2) || (top.size() > 2)) {
    throw new AvroTypeException("Variant types are not supported.");
  }

  int index = in.getCurrentToken() == JsonToken.VALUE_NULL ? nullIndex : typeIndex;
  parser.pushSymbol(top.getSymbol(index));
  return index;
}
 
Example 2
Source File: FastDeserializerGeneratorBase.java    From avro-util with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected FieldAction seekFieldAction(boolean shouldReadCurrent, Schema.Field field,
    ListIterator<Symbol> symbolIterator) {

  Schema.Type type = field.schema().getType();

  if (!shouldReadCurrent) {
    return FieldAction.fromValues(type, false, EMPTY_SYMBOL);
  }

  boolean shouldRead = true;
  Symbol fieldSymbol = END_SYMBOL;

  if (Schema.Type.RECORD.equals(type)) {
    if (symbolIterator.hasNext()) {
      fieldSymbol = symbolIterator.next();
      if (fieldSymbol instanceof Symbol.SkipAction) {
        return FieldAction.fromValues(type, false, fieldSymbol);
      } else {
        symbolIterator.previous();
      }
    }
    return FieldAction.fromValues(type, true, symbolIterator);
  }

  while (symbolIterator.hasNext()) {
    Symbol symbol = symbolIterator.next();

    if (symbol instanceof Symbol.ErrorAction) {
      throw new FastDeserializerGeneratorException(((Symbol.ErrorAction) symbol).msg);
    }

    if (symbol instanceof Symbol.SkipAction) {
      shouldRead = false;
      fieldSymbol = symbol;
      break;
    }

    if (symbol instanceof Symbol.WriterUnionAction) {
      if (symbolIterator.hasNext()) {
        symbol = symbolIterator.next();

        if (symbol instanceof Symbol.Alternative) {
          shouldRead = true;
          fieldSymbol = symbol;
          break;
        }
      }
    }

    if (symbol.kind == Symbol.Kind.TERMINAL) {
      shouldRead = true;
      if (symbolIterator.hasNext()) {
        symbol = symbolIterator.next();

        if (symbol instanceof Symbol.Repeater) {
          fieldSymbol = symbol;
        } else {
          fieldSymbol = symbolIterator.previous();
        }
      } else if (!symbolIterator.hasNext() && getSymbolPrintName(symbol) != null) {
        fieldSymbol = symbol;
      }
      break;
    }
  }

  return FieldAction.fromValues(type, shouldRead, fieldSymbol);
}
 
Example 3
Source File: FastDeserializerGenerator.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void processUnion(JVar unionSchemaVar, final String name, final Schema unionSchema,
    final Schema readerUnionSchema, JBlock body, FieldAction action,
    BiConsumer<JBlock, JExpression> putValueIntoParent, Supplier<JExpression> reuseSupplier) {
  JVar unionIndex = body.decl(codeModel.INT, getUniqueName("unionIndex"), JExpr.direct(DECODER + ".readIndex()"));
  JConditional ifBlock = null;
  for (int i = 0; i < unionSchema.getTypes().size(); i++) {
    Schema optionSchema = unionSchema.getTypes().get(i);
    Schema readerOptionSchema = null;
    FieldAction unionAction;

    if (Schema.Type.NULL.equals(optionSchema.getType())) {
      body._if(unionIndex.eq(JExpr.lit(i)))._then().directStatement(DECODER + ".readNull();");
      continue;
    }

    if (action.getShouldRead()) {
      readerOptionSchema = readerUnionSchema.getTypes().get(i);
      Symbol.Alternative alternative = null;
      if (action.getSymbol() instanceof Symbol.Alternative) {
        alternative = (Symbol.Alternative) action.getSymbol();
      } else if (action.getSymbol().production != null) {
        for (Symbol symbol : action.getSymbol().production) {
          if (symbol instanceof Symbol.Alternative) {
            alternative = (Symbol.Alternative) symbol;
            break;
          }
        }
      }

      if (alternative == null) {
        throw new FastDeserializerGeneratorException("Unable to determine action for field: " + name);
      }

      Symbol.UnionAdjustAction unionAdjustAction = (Symbol.UnionAdjustAction) alternative.symbols[i].production[0];
      unionAction =
          FieldAction.fromValues(optionSchema.getType(), action.getShouldRead(), unionAdjustAction.symToParse);
    } else {
      unionAction = FieldAction.fromValues(optionSchema.getType(), false, EMPTY_SYMBOL);
    }

    JExpression condition = unionIndex.eq(JExpr.lit(i));
    ifBlock = ifBlock != null ? ifBlock._elseif(condition) : body._if(condition);
    final JBlock thenBlock = ifBlock._then();

    JVar optionSchemaVar = null;
    if (useGenericTypes && unionAction.getShouldRead()) {
      JInvocation optionSchemaExpression = unionSchemaVar.invoke("getTypes").invoke("get").arg(JExpr.lit(i));
      optionSchemaVar = declareSchemaVar(optionSchema, name + "OptionSchema", optionSchemaExpression);
    }

    if (SchemaAssistant.isComplexType(optionSchema)) {
      String optionName = name + "Option";
      if (Schema.Type.UNION.equals(optionSchema.getType())) {
        throw new FastDeserializerGeneratorException("Union cannot be sub-type of union!");
      }
      processComplexType(optionSchemaVar, optionName, optionSchema, readerOptionSchema, thenBlock, unionAction,
          putValueIntoParent, reuseSupplier);
    } else {
      processSimpleType(optionSchema, thenBlock, unionAction, putValueIntoParent, reuseSupplier);
    }
  }
}
 
Example 4
Source File: FastDeserializerGeneratorBase.java    From avro-fastserde with Apache License 2.0 4 votes vote down vote up
protected FieldAction seekFieldAction(boolean shouldReadCurrent, Schema.Field field,
        ListIterator<Symbol> symbolIterator) {

    Schema.Type type = field.schema().getType();

    if (!shouldReadCurrent) {
        return FieldAction.fromValues(type, false, EMPTY_SYMBOL);
    }

    boolean shouldRead = true;
    Symbol fieldSymbol = END_SYMBOL;

    if (Schema.Type.RECORD.equals(type)) {
        if (symbolIterator.hasNext()) {
            fieldSymbol = symbolIterator.next();
            if (fieldSymbol instanceof Symbol.SkipAction) {
                return FieldAction.fromValues(type, false, fieldSymbol);
            } else {
                symbolIterator.previous();
            }
        }
        return FieldAction.fromValues(type, true, symbolIterator);
    }

    while (symbolIterator.hasNext()) {
        Symbol symbol = symbolIterator.next();

        if (symbol instanceof Symbol.ErrorAction) {
            throw new FastDeserializerGeneratorException(((Symbol.ErrorAction) symbol).msg);
        }

        if (symbol instanceof Symbol.SkipAction) {
            shouldRead = false;
            fieldSymbol = symbol;
            break;
        }

        if (symbol instanceof Symbol.WriterUnionAction) {
            if (symbolIterator.hasNext()) {
                symbol = symbolIterator.next();

                if (symbol instanceof Symbol.Alternative) {
                    shouldRead = true;
                    fieldSymbol = symbol;
                    break;
                }
            }
        }

        if (symbol.kind == Symbol.Kind.TERMINAL) {
            shouldRead = true;
            if (symbolIterator.hasNext()) {
                symbol = symbolIterator.next();

                if (symbol instanceof Symbol.Repeater) {
                    fieldSymbol = symbol;
                } else {
                    fieldSymbol = symbolIterator.previous();
                }
            } else if (!symbolIterator.hasNext() && getSymbolPrintName(symbol) != null) {
                fieldSymbol = symbol;
            }
            break;
        }
    }

    return FieldAction.fromValues(type, shouldRead, fieldSymbol);
}
 
Example 5
Source File: FastDeserializerGenerator.java    From avro-fastserde with Apache License 2.0 4 votes vote down vote up
private void processUnion(JVar unionSchemaVar, final String name, final Schema unionSchema,
        final Schema readerUnionSchema, JBlock body, FieldAction action,
        BiConsumer<JBlock, JExpression> putValueIntoParent) {
    JVar unionIndex = body.decl(codeModel.INT, getVariableName("unionIndex"),
            JExpr.direct(DECODER + ".readIndex()"));
    JConditional ifBlock = null;
    for (int i = 0; i < unionSchema.getTypes().size(); i++) {
        Schema optionSchema = unionSchema.getTypes().get(i);
        Schema readerOptionSchema = null;
        FieldAction unionAction;

        if (Schema.Type.NULL.equals(optionSchema.getType())) {
            JBlock nullReadBlock = body._if(unionIndex.eq(JExpr.lit(i)))._then().block();
            nullReadBlock.directStatement(DECODER + ".readNull();");
            if (action.getShouldRead()) {
                putValueIntoParent.accept(nullReadBlock, JExpr._null());
            }
            continue;
        }

        if (action.getShouldRead()) {
            readerOptionSchema = readerUnionSchema.getTypes().get(i);
            Symbol.Alternative alternative = null;
            if (action.getSymbol() instanceof Symbol.Alternative) {
                alternative = (Symbol.Alternative) action.getSymbol();
            } else if (action.getSymbol().production != null) {
                for (Symbol symbol : action.getSymbol().production) {
                    if (symbol instanceof Symbol.Alternative) {
                        alternative = (Symbol.Alternative) symbol;
                        break;
                    }
                }
            }

            if (alternative == null) {
                throw new FastDeserializerGeneratorException("Unable to determine action for field: " + name);
            }

            Symbol.UnionAdjustAction unionAdjustAction = (Symbol.UnionAdjustAction) alternative.symbols[i].production[0];
            unionAction = FieldAction.fromValues(optionSchema.getType(), action.getShouldRead(),
                    unionAdjustAction.symToParse);
        } else {
            unionAction = FieldAction.fromValues(optionSchema.getType(), false, EMPTY_SYMBOL);
        }

        JExpression condition = unionIndex.eq(JExpr.lit(i));
        ifBlock = ifBlock != null ? ifBlock._elseif(condition) : body._if(condition);
        final JBlock thenBlock = ifBlock._then();

        JVar optionSchemaVar = null;
        if (useGenericTypes && unionAction.getShouldRead()) {
            JInvocation optionSchemaExpression = unionSchemaVar.invoke("getTypes").invoke("get").arg(JExpr.lit(i));
            optionSchemaVar = declareSchemaVar(optionSchema, name + "OptionSchema", optionSchemaExpression);
        }

        if (SchemaAssistant.isComplexType(optionSchema)) {
            String optionName = name + "Option";
            if (Schema.Type.UNION.equals(optionSchema.getType())) {
                throw new FastDeserializerGeneratorException("Union cannot be sub-type of union!");
            }
            processComplexType(optionSchemaVar, optionName, optionSchema, readerOptionSchema, thenBlock,
                    unionAction, putValueIntoParent);
        } else {
            // to preserve reader string specific options use reader option schema
            if (action.getShouldRead() && Schema.Type.STRING.equals(optionSchema.getType())) {
                processSimpleType(readerOptionSchema, thenBlock, unionAction, putValueIntoParent);

            } else {
                processSimpleType(optionSchema, thenBlock, unionAction, putValueIntoParent);
            }
        }
    }
}