private void parseAndCastNumeric(ATypeTag typeTag, IAType objectType, DataOutput out) throws AsterixException, IOException { final ATypeTag targetTypeTag = getTargetTypeTag(typeTag, objectType); DataOutput dataOutput = out; if (targetTypeTag != typeTag) { castBuffer.reset(); dataOutput = castBuffer.getDataOutput(); } if (targetTypeTag == null || !parseValue(admLexer.getLastTokenImage(), typeTag, dataOutput)) { throw new ParseException( mismatchErrorMessage + objectType.getTypeName() + mismatchErrorMessage2 + typeTag); } // If two type tags are not the same, either we try to promote or demote source type to the // target type if (targetTypeTag != typeTag) { if (ATypeHierarchy.canPromote(typeTag, targetTypeTag)) { // can promote typeTag to targetTypeTag ITypeConvertComputer promoteComputer = ATypeHierarchy.getTypePromoteComputer(typeTag, targetTypeTag); if (promoteComputer == null) { throw new AsterixException( "Can't cast the " + typeTag + " type to the " + targetTypeTag + " type."); } // do the promotion; note that the type tag field should be skipped promoteComputer.convertType( castBuffer.getByteArray(), castBuffer.getStartOffset() + 1, castBuffer.getLength() - 1, out); } else if (ATypeHierarchy.canDemote(typeTag, targetTypeTag)) { // can demote source type to the target type ITypeConvertComputer demoteComputer = ATypeHierarchy.getTypeDemoteComputer(typeTag, targetTypeTag); if (demoteComputer == null) { throw new AsterixException( "Can't cast the " + typeTag + " type to the " + targetTypeTag + " type."); } // do the demotion; note that the type tag field should be skipped demoteComputer.convertType( castBuffer.getByteArray(), castBuffer.getStartOffset() + 1, castBuffer.getLength() - 1, out); } } }
private void parseConstructor(ATypeTag typeTag, IAType objectType, DataOutput out) throws AsterixException, AdmLexerException, IOException { final ATypeTag targetTypeTag = getTargetTypeTag(typeTag, objectType); if (targetTypeTag != null) { DataOutput dataOutput = out; if (targetTypeTag != typeTag) { castBuffer.reset(); dataOutput = castBuffer.getDataOutput(); } int token = admLexer.next(); if (token == AdmLexer.TOKEN_CONSTRUCTOR_OPEN) { token = admLexer.next(); if (token == AdmLexer.TOKEN_STRING_LITERAL) { final String unquoted = admLexer.getLastTokenImage().substring(1, admLexer.getLastTokenImage().length() - 1); if (!parseValue(unquoted, typeTag, dataOutput)) { throw new ParseException( "Missing deserializer method for constructor: " + AdmLexer.tokenKindToString(token) + "."); } token = admLexer.next(); if (token == AdmLexer.TOKEN_CONSTRUCTOR_CLOSE) { if (targetTypeTag != typeTag) { ITypeConvertComputer promoteComputer = ATypeHierarchy.getTypePromoteComputer(typeTag, targetTypeTag); // the availability if the promote computer should be consistent with the availability // of a target type assert promoteComputer != null; // do the promotion; note that the type tag field should be skipped promoteComputer.convertType( castBuffer.getByteArray(), castBuffer.getStartOffset() + 1, castBuffer.getLength() - 1, out); } return; } } } } throw new ParseException( mismatchErrorMessage + objectType.getTypeName() + ". Got " + typeTag + " instead."); }
protected void processPartialResults(IFrameTupleReference tuple) throws AlgebricksException { if (skipStep()) { return; } inputVal.reset(); eval.evaluate(tuple); byte[] serBytes = inputVal.getByteArray(); ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serBytes[0]); switch (typeTag) { case NULL: { processNull(); break; } case SYSTEM_NULL: { // Ignore and return. break; } case RECORD: { // Expected. aggType = ATypeTag.DOUBLE; int nullBitmapSize = 0; int offset1 = ARecordSerializerDeserializer.getFieldOffsetById( serBytes, SUM_FIELD_ID, nullBitmapSize, false); sum += ADoubleSerializerDeserializer.getDouble(serBytes, offset1); int offset2 = ARecordSerializerDeserializer.getFieldOffsetById( serBytes, COUNT_FIELD_ID, nullBitmapSize, false); count += AInt64SerializerDeserializer.getLong(serBytes, offset2); break; } default: { throw new AlgebricksException( "Global-Avg is not defined for values of type " + EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serBytes[0])); } } }
@Override public void finish() throws AlgebricksException { try { switch (aggType) { case NULL: { out.writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG); break; } case SYSTEM_NULL: { finishSystemNull(); break; } default: { out.write(outputVal.getByteArray(), outputVal.getStartOffset(), outputVal.getLength()); break; } } } catch (IOException e) { throw new AlgebricksException(e); } }
static { BOOLEAN_TRUE_CONSTANT = new byte[2]; BOOLEAN_TRUE_CONSTANT[0] = ValueTag.XS_BOOLEAN_TAG; BooleanPointable.setBoolean(BOOLEAN_TRUE_CONSTANT, 1, true); BOOLEAN_FALSE_CONSTANT = new byte[2]; BOOLEAN_FALSE_CONSTANT[0] = ValueTag.XS_BOOLEAN_TAG; BooleanPointable.setBoolean(BOOLEAN_FALSE_CONSTANT, 1, false); ArrayBackedValueStorage abvs = new ArrayBackedValueStorage(); SequenceBuilder sb = new SequenceBuilder(); sb.reset(abvs); try { sb.finish(); } catch (IOException e) { throw new RuntimeException(e); } EMPTY_SEQUENCE = Arrays.copyOf(abvs.getByteArray(), abvs.getLength()); EMPTY_STRING = new byte[3]; EMPTY_STRING[0] = ValueTag.XS_STRING_TAG; EMPTY_STRING[1] = 0; EMPTY_STRING[2] = 0; }
protected void processDataValues(IFrameTupleReference tuple) throws AlgebricksException { if (skipStep()) { return; } inputVal.reset(); eval.evaluate(tuple); ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(inputVal.getByteArray()[0]); if (typeTag == ATypeTag.NULL) { processNull(); return; } else if (aggType == ATypeTag.SYSTEM_NULL) { aggType = typeTag; } else if (typeTag != ATypeTag.SYSTEM_NULL && !ATypeHierarchy.isCompatible(typeTag, aggType)) { throw new AlgebricksException( "Unexpected type " + typeTag + " in aggregation input stream. Expected type " + aggType + "."); } else if (ATypeHierarchy.canPromote(aggType, typeTag)) { aggType = typeTag; } ++count; switch (typeTag) { case INT8: { byte val = AInt8SerializerDeserializer.getByte(inputVal.getByteArray(), 1); sum += val; break; } case INT16: { short val = AInt16SerializerDeserializer.getShort(inputVal.getByteArray(), 1); sum += val; break; } case INT32: { int val = AInt32SerializerDeserializer.getInt(inputVal.getByteArray(), 1); sum += val; break; } case INT64: { long val = AInt64SerializerDeserializer.getLong(inputVal.getByteArray(), 1); sum += val; break; } case FLOAT: { float val = AFloatSerializerDeserializer.getFloat(inputVal.getByteArray(), 1); sum += val; break; } case DOUBLE: { double val = ADoubleSerializerDeserializer.getDouble(inputVal.getByteArray(), 1); sum += val; break; } default: { throw new NotImplementedException("Cannot compute AVG for values of type " + typeTag); } } inputVal.reset(); }
@SuppressWarnings("unchecked") @Override public void evaluate(IFrameTupleReference tuple) throws AlgebricksException { array0.reset(); eval0.evaluate(tuple); array1.reset(); eval1.evaluate(tuple); array2.reset(); eval2.evaluate(tuple); try { if (array0.getByteArray()[0] == SER_NULL_TYPE_TAG || array1.getByteArray()[0] == SER_NULL_TYPE_TAG || array2.getByteArray()[0] == SER_NULL_TYPE_TAG) { nullSerde.serialize(ANull.NULL, dout); return; } else if (array0.getByteArray()[0] != SER_STRING_TYPE_TAG || array1.getByteArray()[0] != SER_STRING_TYPE_TAG || array2.getByteArray()[0] != SER_STRING_TYPE_TAG) { throw new AlgebricksException( funcID.getName() + ": expects input type (STRING/NULL, STRING/NULL, STRING/NULL), but got (" + EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(array0.getByteArray()[0]) + ", " + EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(array1.getByteArray()[0]) + ", " + EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(array2.getByteArray()[0]) + "."); } } catch (HyracksDataException e) { throw new AlgebricksException(e); } strPtr1st.set(array0.getByteArray(), array0.getStartOffset() + 1, array0.getLength()); strPtr2nd.set(array1.getByteArray(), array1.getStartOffset() + 1, array1.getLength()); strPtr3rd.set(array2.getByteArray(), array2.getStartOffset() + 1, array2.getLength()); String res = compute(strPtr1st, strPtr2nd, strPtr3rd); resultBuffer.setValue(res); try { strSerde.serialize(resultBuffer, dout); } catch (HyracksDataException e) { throw new AlgebricksException(e); } }
@Override public void step(IFrameTupleReference tuple) throws AlgebricksException { inputVal.reset(); eval.evaluate(tuple); ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(inputVal.getByteArray()[0]); if (typeTag == ATypeTag.NULL) { processNull(); return; } else if (aggType == ATypeTag.NULL) { return; } else if (aggType == ATypeTag.SYSTEM_NULL) { if (typeTag == ATypeTag.SYSTEM_NULL) { // Ignore. return; } // First value encountered. Set type, comparator, and initial value. aggType = typeTag; // Set comparator. IBinaryComparatorFactory cmpFactory = AqlBinaryComparatorFactoryProvider.INSTANCE.getBinaryComparatorFactory(aggType, isMin); cmp = cmpFactory.createBinaryComparator(); // Initialize min value. outputVal.assign(inputVal); } else if (typeTag != ATypeTag.SYSTEM_NULL && !ATypeHierarchy.isCompatible(typeTag, aggType)) { throw new AlgebricksException( "Unexpected type " + typeTag + " in aggregation input stream. Expected type " + aggType + "."); } else { // If a system_null is encountered locally, it would be an error; otherwise if it is seen // by a global aggregator, it is simple ignored. if (typeTag == ATypeTag.SYSTEM_NULL) { processSystemNull(); return; } if (ATypeHierarchy.canPromote(aggType, typeTag)) { tpc = ATypeHierarchy.getTypePromoteComputer(aggType, typeTag); aggType = typeTag; cmp = AqlBinaryComparatorFactoryProvider.INSTANCE .getBinaryComparatorFactory(aggType, isMin) .createBinaryComparator(); if (tpc != null) { tempValForCasting.reset(); try { tpc.convertType( outputVal.getByteArray(), outputVal.getStartOffset() + 1, outputVal.getLength() - 1, tempValForCasting.getDataOutput()); } catch (IOException e) { throw new AlgebricksException(e); } outputVal.reset(); outputVal.assign(tempValForCasting); } try { if (cmp.compare( inputVal.getByteArray(), inputVal.getStartOffset(), inputVal.getLength(), outputVal.getByteArray(), outputVal.getStartOffset(), outputVal.getLength()) < 0) { outputVal.assign(inputVal); } } catch (HyracksDataException e) { throw new AlgebricksException(e); } } else { tpc = ATypeHierarchy.getTypePromoteComputer(typeTag, aggType); if (tpc != null) { tempValForCasting.reset(); try { tpc.convertType( inputVal.getByteArray(), inputVal.getStartOffset() + 1, inputVal.getLength() - 1, tempValForCasting.getDataOutput()); } catch (IOException e) { throw new AlgebricksException(e); } try { if (cmp.compare( tempValForCasting.getByteArray(), tempValForCasting.getStartOffset(), tempValForCasting.getLength(), outputVal.getByteArray(), outputVal.getStartOffset(), outputVal.getLength()) < 0) { outputVal.assign(tempValForCasting); } } catch (HyracksDataException e) { throw new AlgebricksException(e); } } else { try { if (cmp.compare( inputVal.getByteArray(), inputVal.getStartOffset(), inputVal.getLength(), outputVal.getByteArray(), outputVal.getStartOffset(), outputVal.getLength()) < 0) { outputVal.assign(inputVal); } } catch (HyracksDataException e) { throw new AlgebricksException(e); } } } } }
private void parseRecord(ARecordType recType, DataOutput out, Boolean datasetRec) throws IOException, AsterixException, AdmLexerException { ArrayBackedValueStorage fieldValueBuffer = getTempBuffer(); ArrayBackedValueStorage fieldNameBuffer = getTempBuffer(); IARecordBuilder recBuilder = getRecordBuilder(); BitSet nulls = null; if (datasetRec) { if (recType != null) { nulls = new BitSet(recType.getFieldNames().length); recBuilder.reset(recType); } else { recBuilder.reset(null); } } else if (recType != null) { nulls = new BitSet(recType.getFieldNames().length); recBuilder.reset(recType); } else { recBuilder.reset(null); } recBuilder.init(); int token; boolean inRecord = true; boolean expectingRecordField = false; boolean first = true; Boolean openRecordField = false; int fieldId = 0; IAType fieldType = null; do { token = admLexer.next(); switch (token) { case AdmLexer.TOKEN_END_RECORD: { if (expectingRecordField) { throw new ParseException("Found END_RECORD while expecting a record field."); } inRecord = false; break; } case AdmLexer.TOKEN_STRING_LITERAL: { // we've read the name of the field // now read the content fieldNameBuffer.reset(); fieldValueBuffer.reset(); expectingRecordField = false; if (recType != null) { String fldName = admLexer .getLastTokenImage() .substring(1, admLexer.getLastTokenImage().length() - 1); fieldId = recBuilder.getFieldId(fldName); if (fieldId < 0 && !recType.isOpen()) { throw new ParseException("This record is closed, you can not add extra fields !!"); } else if (fieldId < 0 && recType.isOpen()) { aStringFieldName.setValue( admLexer .getLastTokenImage() .substring(1, admLexer.getLastTokenImage().length() - 1)); stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput()); openRecordField = true; fieldType = null; } else { // a closed field nulls.set(fieldId); fieldType = recType.getFieldTypes()[fieldId]; openRecordField = false; } } else { aStringFieldName.setValue( admLexer .getLastTokenImage() .substring(1, admLexer.getLastTokenImage().length() - 1)); stringSerde.serialize(aStringFieldName, fieldNameBuffer.getDataOutput()); openRecordField = true; fieldType = null; } token = admLexer.next(); if (token != AdmLexer.TOKEN_COLON) { throw new ParseException( "Unexpected ADM token kind: " + AdmLexer.tokenKindToString(token) + " while expecting \":\"."); } token = admLexer.next(); this.admFromLexerStream(token, fieldType, fieldValueBuffer.getDataOutput(), false); if (openRecordField) { if (fieldValueBuffer.getByteArray()[0] != ATypeTag.NULL.serialize()) { recBuilder.addField(fieldNameBuffer, fieldValueBuffer); } } else if (NonTaggedFormatUtil.isOptional(recType)) { if (fieldValueBuffer.getByteArray()[0] != ATypeTag.NULL.serialize()) { recBuilder.addField(fieldId, fieldValueBuffer); } } else { recBuilder.addField(fieldId, fieldValueBuffer); } break; } case AdmLexer.TOKEN_COMMA: { if (first) { throw new ParseException("Found COMMA before any record field."); } if (expectingRecordField) { throw new ParseException("Found COMMA while expecting a record field."); } expectingRecordField = true; break; } default: { throw new ParseException( "Unexpected ADM token kind: " + AdmLexer.tokenKindToString(token) + " while parsing record fields."); } } first = false; } while (inRecord); if (recType != null) { nullableFieldId = checkNullConstraints(recType, nulls); if (nullableFieldId != -1) { throw new ParseException( "Field: " + recType.getFieldNames()[nullableFieldId] + " can not be null"); } } recBuilder.write(out, true); }