Package sizzle.types

Examples of sizzle.types.SizzleFunction


    this.functions = new FunctionTrie();

    // these generic functions require more finagling than can currently be
    // (easily) done with a static method, so they are handled with macros

    this.setFunction("def", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleAny() }, "${0} != null"));
    this.setFunction("len", new SizzleFunction(new SizzleInt(), new SizzleType[] { new SizzleArray(new SizzleScalar()) }, "${0}.length"));
    this.setFunction("len", new SizzleFunction(new SizzleInt(), new SizzleType[] { new SizzleString() }, "${0}.length()"));
    this.setFunction("len", new SizzleFunction(new SizzleInt(), new SizzleType[] { new SizzleBytes() }, "${0}.length"));
    this.setFunction("len", new SizzleFunction(new SizzleInt(), new SizzleType[] { new SizzleMap(new SizzleScalar(), new SizzleScalar()) },
        "${0}.keySet().size()"));
    this.setFunction("haskey", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleMap(new SizzleScalar(), new SizzleScalar()),
        new SizzleScalar() }, "${0}.containsKey(${1})"));
    this.setFunction("keys", new SizzleFunction(new SizzleArray(new SizzleScalar()), new SizzleType[] { new SizzleMap(new SizzleScalar(),
        new SizzleScalar()) }, "${0}.keySet().toArray()"));
    this.setFunction("lookup", new SizzleFunction(new SizzleScalar(), new SizzleType[] { new SizzleMap(new SizzleScalar(), new SizzleScalar()),
        new SizzleScalar(), new SizzleScalar() }, "(${0}.containsKey(${1}) ? ${0}.get(${1}) : ${2})"));

    this.setFunction("regex", new SizzleFunction(new SizzleString(), new SizzleType[] { new SizzleName(new SizzleScalar()), new SizzleInt() },
        "sizzle.functions.SizzleSpecialIntrinsics.regex(\"${0}\", ${1})"));
    this.setFunction("regex", new SizzleFunction(new SizzleString(), new SizzleType[] { new SizzleName(new SizzleScalar()) },
        "sizzle.functions.SizzleSpecialIntrinsics.regex(\"${0}\")"));
    // these fingerprints are identity functions
    this.setFunction("fingerprintof", new SizzleFunction(new SizzleFingerprint(), new SizzleScalar[] { new SizzleInt() }));
    this.setFunction("fingerprintof", new SizzleFunction(new SizzleFingerprint(), new SizzleScalar[] { new SizzleTime() }));

    /* expose all the casting constructors to Sawzall */

    // string to bool
    this.setFunction("bool",
        new SizzleFunction("sizzle.functions.SizzleCasts.stringToBoolean", new SizzleBool(), new SizzleScalar[] { new SizzleString() }));

    // bool to int
    this.setFunction("int", new SizzleFunction("sizzle.functions.SizzleCasts.booleanToLong", new SizzleInt(), new SizzleScalar[] { new SizzleBool() }));
    // float to int
    this.setFunction("int", new SizzleFunction(new SizzleInt(), new SizzleScalar[] { new SizzleFloat() }, "(long)${0}"));
    // time to int
    this.setFunction("int", new SizzleFunction(new SizzleInt(), new SizzleScalar[] { new SizzleTime() }));
    // fingerprint to int
    this.setFunction("int", new SizzleFunction(new SizzleInt(), new SizzleScalar[] { new SizzleFingerprint() }));
    // string to int
    this.setFunction("int", new SizzleFunction("java.lang.Long.decode", new SizzleInt(), new SizzleScalar[] { new SizzleString() }));
    // string to int with param base
    this.setFunction("int", new SizzleFunction(new SizzleInt(), new SizzleScalar[] { new SizzleString(), new SizzleInt() },
        "java.lang.Long.parseLong(${0}, (int)${1})"));
    // bytes to int with param encoding format
    this.setFunction("int", new SizzleFunction("sizzle.functions.SizzleCasts.bytesToLong", new SizzleInt(), new SizzleScalar[] { new SizzleBytes(),
        new SizzleString() }));

    // int to float
    this.setFunction("float", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleInt() }, "(double)${0}"));
    // string to float
    this.setFunction("float", new SizzleFunction("java.lang.Double.parseDouble", new SizzleFloat(), new SizzleScalar[] { new SizzleString() }));

    // int to time
    this.setFunction("time", new SizzleFunction(new SizzleTime(), new SizzleScalar[] { new SizzleInt() }));
    // string to time
    this.setFunction("time", new SizzleFunction("sizzle.functions.SizzleCasts.stringToTime", new SizzleTime(), new SizzleScalar[] { new SizzleString() }));
    // string to time
    this.setFunction("time", new SizzleFunction("sizzle.functions.SizzleCasts.stringToTime", new SizzleTime(), new SizzleScalar[] { new SizzleString(),
        new SizzleString() }));

    // int to fingerprint
    this.setFunction("fingerprint", new SizzleFunction(new SizzleFingerprint(), new SizzleScalar[] { new SizzleInt() }));
    // string to fingerprint
    this.setFunction("fingerprint", new SizzleFunction("java.lang.Long.parseLong", new SizzleInt(), new SizzleScalar[] { new SizzleString() }));
    // string to fingerprint with param base
    this.setFunction("fingerprint", new SizzleFunction("java.lang.Long.parseLong", new SizzleInt(), new SizzleScalar[] { new SizzleString(),
        new SizzleInt() }));
    // bytes to fingerprint
    this.setFunction("fingerprint", new SizzleFunction("sizzle.functions.SizzleCasts.bytesToFingerprint", new SizzleFingerprint(),
        new SizzleScalar[] { new SizzleBytes() }));

    // bool to string
    this.setFunction("string", new SizzleFunction("java.lang.Boolean.toString", new SizzleString(), new SizzleScalar[] { new SizzleBool() }));
    // int to string
    this.setFunction("string", new SizzleFunction("java.lang.Long.toString", new SizzleString(), new SizzleScalar[] { new SizzleInt() }));
    // int to string with parameter base
    this.setFunction("string", new SizzleFunction("sizzle.functions.SizzleCasts.longToString", new SizzleString(), new SizzleScalar[] { new SizzleInt(),
        new SizzleInt() }));
    // float to string
    this.setFunction("string", new SizzleFunction("java.lang.Double.toString", new SizzleString(), new SizzleScalar[] { new SizzleFloat() }));
    // time to string
    this.setFunction("string", new SizzleFunction("sizzle.functions.SizzleCasts.timeToString", new SizzleString(), new SizzleScalar[] { new SizzleTime() }));
    // fingerprint to string
    this.setFunction("string", new SizzleFunction("java.lang.Long.toHexString", new SizzleString(), new SizzleScalar[] { new SizzleFingerprint() }));
    // bytes to string
    this.setFunction("string", new SizzleFunction("new java.lang.String", new SizzleString(), new SizzleScalar[] { new SizzleBytes() }));
    // bytes to string
    this.setFunction("string", new SizzleFunction("new java.lang.String", new SizzleString(), new SizzleScalar[] { new SizzleBytes(), new SizzleString() }));

    // int to bytes with param encoding format
    this.setFunction("bytes", new SizzleFunction("sizzle.functions.SizzleCasts.longToBytes", new SizzleInt(), new SizzleScalar[] { new SizzleInt(),
        new SizzleString() }));
    // fingerprint to bytes
    this.setFunction("bytes", new SizzleFunction("sizzle.functions.SizzleCasts.fingerprintToBytes", new SizzleBytes(),
        new SizzleScalar[] { new SizzleFingerprint() }));
    // string to bytes
    this.setFunction("bytes",
        new SizzleFunction("sizzle.functions.SizzleCasts.stringToBytes", new SizzleBytes(), new SizzleScalar[] { new SizzleString() }));

    /* expose the java.lang.Math class to Sawzall */

    this.setFunction("highbit", new SizzleFunction("java.lang.Long.highestOneBit", new SizzleInt(), new SizzleScalar[] { new SizzleInt() }));

    // abs just needs to be overloaded
    this.setFunction("abs", new SizzleFunction("java.lang.Math.abs", new SizzleFloat(), new SizzleScalar[] { new SizzleInt() }));
    this.setFunction("abs", new SizzleFunction("java.lang.Math.abs", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

    // abs is also named fabs in Sawzall
    this.setFunction("fabs", new SizzleFunction("java.lang.Math.abs", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

    // log is named ln in Sawzall
    this.setFunction("ln", new SizzleFunction("java.lang.Math.log", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

    // expose the rest of the unary functions
    for (final String s : Arrays.asList("log10", "exp", "sqrt", "sin", "cos", "tan", "asin", "acos", "atan", "cosh", "sinh", "tanh", "ceil", "floor",
        "round"))
      this.setFunction(s, new SizzleFunction("java.lang.Math." + s, new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

    // expose the binary functions
    for (final String s : Arrays.asList("pow", "atan2"))
      this.setFunction(s, new SizzleFunction("java.lang.Math." + s, new SizzleFloat(), new SizzleScalar[] { new SizzleFloat(), new SizzleFloat() }));

    for (final String s : Arrays.asList("max", "min"))
      for (final SizzleScalar t : Arrays.asList(new SizzleInt(), new SizzleFloat()))
        this.setFunction(s, new SizzleFunction("java.lang.Math." + s, new SizzleFloat(), new SizzleScalar[] { t, t }));

    this.setFunction("max", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleTime(), new SizzleTime() }, "(${0} > ${1} ? ${0} : ${1})"));
    this.setFunction("min", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleTime(), new SizzleTime() }, "(${0} < ${1} ? ${0} : ${1})"));

    this.setFunction("max", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleString(), new SizzleString() },
        "(${0}.compareTo(${1}) > 0 ? ${0} : ${1})"));
    this.setFunction("min", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleString(), new SizzleString() },
        "(${0}.compareTo(${1}) < 0 ? ${0} : ${1})"));

    // expose whatever is left, assuming we are not aiming for strict
    // compatibility
    if (!this.strictCompatibility) {
      // random takes no argument

      // these three have capitals in the name
      this.setFunction("ieeeremainder", new SizzleFunction("java.lang.Math.IEEEremainder", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat(),
          new SizzleFloat() }));
      this.setFunction("todegrees", new SizzleFunction("java.lang.Math.toDegrees", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));
      this.setFunction("toradians", new SizzleFunction("java.lang.Math.toRadians", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

      // the unaries
      for (final String s : Arrays.asList("cbrt", "expm1", "log1p", "rint", "signum", "ulp"))
        this.setFunction(s, new SizzleFunction("java.lang.Math." + s, new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

      // and binaries
      this.setFunction("hypot",
          new SizzleFunction("java.lang.Math.hypot", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat(), new SizzleFloat() }));
    }

    // add in the default tables
    // FIXME: support format strings and files
    this.set("stdout", new SizzleTable(new SizzleString()));
View Full Code Here


        this.importAvro(dep);
      else
        throw new TypeException("unknown dependency in " + dep);

    this.setFunction(annotation.name(),
        new SizzleFunction(m.getDeclaringClass().getCanonicalName() + '.' + m.getName(), this.getType(annotation.returnType()), formalParameterTypes));
  }
View Full Code Here

  public SizzleFunction getFunction(final String id, final List<SizzleType> formalParameters) {
    return this.getFunction(id, formalParameters.toArray(new SizzleType[formalParameters.size()]));
  }

  public SizzleFunction getFunction(final String id, final SizzleType[] formalParameters) {
    final SizzleFunction func = this.functions.getFunction(id, formalParameters);

    return func;
  }
View Full Code Here

public class TestFunctionTrie {
  @Test
  public void testFunctionTrieSingleParameter() {
    final FunctionTrie functionTrie = new FunctionTrie();

    final SizzleFunction sizzleFunction = new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleString() });

    functionTrie.addFunction("function", sizzleFunction);
    functionTrie.addFunction("function", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleInt() }));

    Assert.assertEquals("did not return correct function", sizzleFunction, functionTrie.getFunction("function", new SizzleType[] { new SizzleString() }));
  }
View Full Code Here

  @Test
  public void testFunctionTrieMultiParameter() {
    final FunctionTrie functionTrie = new FunctionTrie();

    final SizzleFunction sizzleFunction = new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleInt(), new SizzleFloat(), new SizzleString() });

    functionTrie.addFunction("function", sizzleFunction);
    functionTrie.addFunction("function", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleString(), new SizzleFloat(), new SizzleInt() }));

    Assert.assertEquals("did not return correct function", sizzleFunction,
        functionTrie.getFunction("function", new SizzleType[] { new SizzleInt(), new SizzleFloat(), new SizzleString() }));
  }
View Full Code Here

  @Test
  public void testFunctionTrieOverloadedArgsShort() {
    final FunctionTrie functionTrie = new FunctionTrie();

    final SizzleFunction sizzleFunction = new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleInt() });

    functionTrie.addFunction("function", sizzleFunction);
    functionTrie.addFunction("function", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleInt(), new SizzleString() }));

    Assert.assertEquals("did not return correct function", sizzleFunction, functionTrie.getFunction("function", new SizzleType[] { new SizzleInt() }));
  }
View Full Code Here

  @Test
  public void testFunctionTrieOverloadedArgsLong() {
    final FunctionTrie functionTrie = new FunctionTrie();

    final SizzleFunction sizzleFunction = new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleInt(), new SizzleString() });

    functionTrie.addFunction("function", sizzleFunction);
    functionTrie.addFunction("function", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleInt() }));

    Assert.assertEquals("did not return correct function", sizzleFunction,
        functionTrie.getFunction("function", new SizzleType[] { new SizzleInt(), new SizzleString() }));
  }
View Full Code Here

  @Test
  public void testFunctionTrieGeneric() {
    final FunctionTrie functionTrie = new FunctionTrie();

    final SizzleFunction sizzleFunction = new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleAny() });

    functionTrie.addFunction("function", sizzleFunction);
    functionTrie.addFunction("function", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleInt() }));

    Assert.assertEquals("did not return correct function", sizzleFunction, functionTrie.getFunction("function", new SizzleType[] { new SizzleString() }));
  }
View Full Code Here

  @Test
  public void testFunctionTrieVarargs() {
    final FunctionTrie functionTrie = new FunctionTrie();

    final SizzleFunction sizzleFunction = new SizzleFunction(new SizzleBool(),
        new SizzleType[] { new SizzleString(), new SizzleVarargs(new SizzleString()) });

    functionTrie.addFunction("function", sizzleFunction);
    functionTrie.addFunction("function", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleString(), new SizzleVarargs(new SizzleInt()) }));

    Assert.assertEquals("did not return correct function", sizzleFunction,
        functionTrie.getFunction("function", new SizzleType[] { new SizzleString(), new SizzleString(), new SizzleString(), new SizzleString() }));
  }
View Full Code Here

    final String source = "out: table sum[key: string][month: int][day: int] of int;\nstatic keywords: array of string = { \"hitchhiker\", \"benedict\", \"vytorin\", \"itanium\", \"aardvark\" };\nquerywords: array of string = words_from_query();\nmonth: int = month_of_query();\nday: int = day_of_query();\nwhen (i: each int; j: some int; querywords[i] == keywords[j])\n    emit out[keywords[j]][month][day] <- 1;\n";

    final SymbolTable st = new SymbolTable();

    // fake functions for this unit test
    st.setFunction("day_of_query", new SizzleFunction(new SizzleInt(), new SizzleType[] {}, "Nonexistant.day_of_query()"));
    st.setFunction("month_of_query", new SizzleFunction(new SizzleInt(), new SizzleType[] {}, "Nonexistant.month_of_query()"));
    st.setFunction("words_from_query", new SizzleFunction(new SizzleArray(new SizzleString()), new SizzleType[] {}, "Nonexistant.words_from_query()"));

    SizzleParser.ReInit(new StringReader(source));
    TestTypeCheckingVisitor.typeChecker.visit(SizzleParser.Start(), st);

    Assert.assertEquals("out is not an unweighted table of ints indexed by various",
View Full Code Here

TOP

Related Classes of sizzle.types.SizzleFunction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.