Package sql.schema.parser

Source Code of sql.schema.parser.SchemaParser$TableInfo

/* Generated By:JavaCC: Do not edit this line. SchemaParser.java */
/** Schema + cardinality information extractor. */
package sql.schema.parser;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import plan_runner.conversion.DateConversion;
import plan_runner.conversion.DoubleConversion;
import plan_runner.conversion.LongConversion;
import plan_runner.conversion.StringConversion;
import plan_runner.conversion.TypeConversion;
import sql.schema.ColumnNameType;

public class SchemaParser implements SchemaParserConstants {
  public static class ColumnInfo {

    private String _name;
    private TypeConversion _type;
    private long _distinctValues = INVALID;
    private Object _minValue, _maxValue; // used for ranges

    private static final TypeConversion _lc = new LongConversion();
    private static final TypeConversion _dbc = new DoubleConversion();
    private static final TypeConversion _sc = new StringConversion();
    private static final TypeConversion _dtc = new DateConversion();

    public long getDistinctValues() {
      return _distinctValues;
    }

    public Object getMaxValue() {
      return _maxValue;
    }

    public Object getMinValue() {
      return _minValue;
    }

    public String getName() {
      return _name;
    }

    public TypeConversion getType() {
      return _type;
    }

    public void setDistinctValues(long distinctValues) {
      _distinctValues = distinctValues;
    }

    public void setMaxValue(Object maxValue) {
      _maxValue = maxValue;
    }

    public void setMinValue(Object minValue) {
      _minValue = minValue;
    }

    public void setName(String name) {
      _name = name;
    }

    public void setType(String type) {
      _type = strToTypeConv(type);
    }

    public TypeConversion strToTypeConv(String type) {
      if (type.equalsIgnoreCase("LONG"))
        return _lc;
      else if (type.equalsIgnoreCase("DOUBLE"))
        return _dbc;
      else if (type.equalsIgnoreCase("STRING"))
        return _sc;
      else if (type.equalsIgnoreCase("DATE"))
        return _dtc;
      else
        throw new RuntimeException("Error setting type for column " + _name
            + ". Only LONG, DOUBLE, STRING or DATE are allowed.");
    }

    @Override
    public String toString() {
      final StringBuilder sb = new StringBuilder();
      sb.append(_name).append(" of type ").append(_type);
      sb.append(" Distinct Values = ").append(_distinctValues);
      sb.append(" Range [").append(_minValue).append(", ").append(_maxValue).append("]");
      return sb.toString();
    }
  }

  public static class TableInfo {

    private String _name;
    private long _tableSize = INVALID;
    private final Map<String, ColumnInfo> _columns = new HashMap<String, ColumnInfo>();

    // we need to provide efficient access to tableSchema
    private final List<ColumnNameType> _cnts = new ArrayList<ColumnNameType>();

    public void addColumn(ColumnInfo ci) {
      _columns.put(ci.getName(), ci);
      _cnts.add(new ColumnNameType(ci.getName(), ci.getType()));
    }

    public Map<String, ColumnInfo> getColumnInfos() {
      return _columns;
    }

    public String getName() {
      return _name;
    }

    public List<ColumnNameType> getTableSchema() {
      return _cnts;
    }

    public long getTableSize() {
      return _tableSize;
    }

    public void setName(String name) {
      _name = name;
    }

    public void setTableSize(long tableSize) {
      _tableSize = tableSize;
    }

    @Override
    public String toString() {
      final StringBuilder sb = new StringBuilder();
      sb.append("\u005cnTABLE NAME ").append(_name);
      sb.append(", SIZE ").append(_tableSize);
      sb.append("\u005cn  with COLUMNS:\u005cn");
      for (final Map.Entry<String, ColumnInfo> entry : _columns.entrySet())
        sb.append("    ").append(entry.getValue()).append("\u005cn");
      return sb.toString();
    }
  }

  private static Logger LOG = Logger.getLogger(SchemaParser.class);

  public static long INVALID = -1;

  public static InputStream getFileInputStream(String path) {
    // create file object
    final File file = new File(path);

    FileInputStream fin = null;

    try {
      fin = new FileInputStream(file);
    } catch (final FileNotFoundException e) {
      LOG.info("File " + file.getAbsolutePath() + " could not be found on filesystem");
    }

    return fin;
  }

  /** Printing for debugging purposes. */
  public static String getParsedString(Map<String, TableInfo> tables) {
    final StringBuilder sb = new StringBuilder("PARSED result:");
    for (final Map.Entry<String, TableInfo> entry : tables.entrySet())
      sb.append(entry.getValue());
    return sb.toString();
  }

  public static Map<String, TableInfo> getSchemaInfo(String path, double scallingFactor)
      throws ParseException {
    final SchemaParser parser = new SchemaParser(getFileInputStream(path));
    return parser.Input(scallingFactor);
  }

  /** Main entry point. */
  public static void main(String args[]) throws ParseException {
    final String path = args[0];
    final double scallingFactor = Double.valueOf(args[1]);
    final Map<String, TableInfo> tables = getSchemaInfo(path, scallingFactor);
    LOG.info(getParsedString(tables));
  }

  /** Generated Token Manager. */
  public SchemaParserTokenManager token_source;

  SimpleCharStream jj_input_stream;

  /** Current token. */
  public Token token;

  /** Next token. */
  public Token jj_nt;

  private int jj_ntk;

  private int jj_gen;

  final private int[] jj_la1 = new int[8];

  static private int[] jj_la1_0;

  static {
    jj_la1_init_0();
  }

  private static void jj_la1_init_0() {
    jj_la1_0 = new int[] { 0x8000, 0x1000, 0x1c0000, 0x1000, 0x80, 0x400, 0x16000, 0x16000, };
  }

  private final java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
  private int[] jj_expentry;
  private int jj_kind = -1;

  /** Constructor with InputStream. */
  public SchemaParser(java.io.InputStream stream) {
    this(stream, null);
  }

  /** Constructor with InputStream and supplied encoding */
  public SchemaParser(java.io.InputStream stream, String encoding) {
    try {
      jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1);
    } catch (final java.io.UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
    token_source = new SchemaParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 8; i++)
      jj_la1[i] = -1;
  }

  /** Constructor. */
  public SchemaParser(java.io.Reader stream) {
    jj_input_stream = new SimpleCharStream(stream, 1, 1);
    token_source = new SchemaParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 8; i++)
      jj_la1[i] = -1;
  }

  /** Constructor with generated Token Manager. */
  public SchemaParser(SchemaParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 8; i++)
      jj_la1[i] = -1;
  }

  /** Disable tracing. */
  final public void disable_tracing() {
  }

  final public void DistinctValues(ColumnInfo ci, double scallingFactor) throws ParseException {
    long numValue;
    jj_consume_token(DISTINCTVAL);
    jj_consume_token(EQ);
    numValue = MatchedLong(scallingFactor);
    ci.setDistinctValues(numValue);
  }

  /** Enable tracing. */
  final public void enable_tracing() {
  }

  /** Generate ParseException. */
  public ParseException generateParseException() {
    jj_expentries.clear();
    final boolean[] la1tokens = new boolean[21];
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 8; i++)
      if (jj_la1[i] == jj_gen)
        for (int j = 0; j < 32; j++)
          if ((jj_la1_0[i] & (1 << j)) != 0)
            la1tokens[j] = true;
    for (int i = 0; i < 21; i++)
      if (la1tokens[i]) {
        jj_expentry = new int[1];
        jj_expentry[0] = i;
        jj_expentries.add(jj_expentry);
      }
    final int[][] exptokseq = new int[jj_expentries.size()][];
    for (int i = 0; i < jj_expentries.size(); i++)
      exptokseq[i] = jj_expentries.get(i);
    return new ParseException(token, exptokseq, tokenImage);
  }

  /** Get the next Token. */
  final public Token getNextToken() {
    if (token.next != null)
      token = token.next;
    else
      token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    jj_gen++;
    return token;
  }

  /** Get the specific Token. */
  final public Token getToken(int index) {
    Token t = token;
    for (int i = 0; i < index; i++)
      if (t.next != null)
        t = t.next;
      else
        t = t.next = token_source.getNextToken();
    return t;
  }

  /** Root production. */
  final public Map<String, TableInfo> Input(double scallingFactor) throws ParseException {
    final Map<String, TableInfo> _tables = new HashMap<String, TableInfo>();
    TableInfo ti;
    label_1: while (true) {
      ti = MatchedTable(scallingFactor);
      _tables.put(ti.getName(), ti);
      switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
      case TABLE:
        ;
        break;
      default:
        jj_la1[0] = jj_gen;
        break label_1;
      }
    }
    jj_consume_token(0);
    {
      if (true)
        return _tables;
    }
    throw new Error("Missing return statement in function");
  }

  private Token jj_consume_token(int kind) throws ParseException {
    Token oldToken;
    if ((oldToken = token).next != null)
      token = token.next;
    else
      token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    if (token.kind == kind) {
      jj_gen++;
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  }

  private int jj_ntk() {
    if ((jj_nt = token.next) == null)
      return (jj_ntk = (token.next = token_source.getNextToken()).kind);
    else
      return (jj_ntk = jj_nt.kind);
  }

  /** Column recognition */
  final public ColumnInfo MatchedColumn(double scallingFactor) throws ParseException {
    final ColumnInfo ci = new ColumnInfo();
    Token token;
    token = jj_consume_token(ID);
    ci.setName(token.image);
    token = jj_consume_token(TYPE);
    ci.setType(token.image);
    OptionalColumn(ci, scallingFactor);
    {
      if (true)
        return ci;
    }
    throw new Error("Missing return statement in function");
  }

  /** Matching long and any other value types. */
  final public long MatchedLong(double scallingFactor) throws ParseException {
    Token token;
    long value;
    token = jj_consume_token(LONG_NUM);
    value = Long.valueOf(token.image);
    switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
    case SCALLED:
      jj_consume_token(SCALLED);
      value = (long) (value * scallingFactor);
      break;
    default:
      jj_la1[1] = jj_gen;
      ;
    }
    {
      if (true)
        return value;
    }
    throw new Error("Missing return statement in function");
  }

  final public Object MatchedObject(ColumnInfo ci, double scallingFactor) throws ParseException {
    Token token;
    Object value;
    switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
    case LONG_NUM:
      token = jj_consume_token(LONG_NUM);
      value = ci.getType().fromString(token.image);
      break;
    case ID:
      token = jj_consume_token(ID);
      value = ci.getType().fromString(token.image);
      break;
    case STR:
      token = jj_consume_token(STR);
      break;
    default:
      jj_la1[2] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    value = ci.getType().fromString(token.image);
    switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
    case SCALLED:
      jj_consume_token(SCALLED);
      final TypeConversion tc = ci.getType();
      if (tc instanceof LongConversion) {
        final LongConversion lc = (LongConversion) tc;
        final Long lvalue = (Long) value;
        value = (long) (lc.toDouble(lvalue) * scallingFactor);
      } else if (tc instanceof DoubleConversion) {
        final DoubleConversion dbc = (DoubleConversion) tc;
        final Double dvalue = (Double) value;
        value = (double) (dbc.toDouble(dvalue) * scallingFactor);
      } else {
        if (true)
          throw new RuntimeException("Only Long and Double can be scalled out!");
      }
      break;
    default:
      jj_la1[3] = jj_gen;
      ;
    }
    {
      if (true)
        return value;
    }
    throw new Error("Missing return statement in function");
  }

  /** Table recognition */
  final public TableInfo MatchedTable(double scallingFactor) throws ParseException {
    final TableInfo ti = new TableInfo();
    ColumnInfo ci;
    Token token;
    long numValue;
    jj_consume_token(TABLE);
    token = jj_consume_token(ID);
    ti.setName(token.image);
    switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
    case LSQUARE:
      jj_consume_token(LSQUARE);
      numValue = MatchedLong(scallingFactor);
      jj_consume_token(RSQUARE);
      ti.setTableSize(numValue);
      break;
    default:
      jj_la1[4] = jj_gen;
      ;
    }
    jj_consume_token(LPAREN);
    ci = MatchedColumn(scallingFactor);
    ti.addColumn(ci);
    label_2: while (true) {
      switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
      case COL_DELIM:
        ;
        break;
      default:
        jj_la1[5] = jj_gen;
        break label_2;
      }
      jj_consume_token(COL_DELIM);
      ci = MatchedColumn(scallingFactor);
      ti.addColumn(ci);
    }
    jj_consume_token(RPAREN);
    jj_consume_token(TABLE_DELIM);
    {
      if (true)
        return ti;
    }
    throw new Error("Missing return statement in function");
  }

  final public void MaxValue(ColumnInfo ci, double scallingFactor) throws ParseException {
    Object obj;
    jj_consume_token(MAXVAL);
    jj_consume_token(EQ);
    obj = MatchedObject(ci, scallingFactor);
    ci.setMaxValue(obj);
  }

  final public void MinValue(ColumnInfo ci, double scallingFactor) throws ParseException {
    Object obj;
    jj_consume_token(MINVAL);
    jj_consume_token(EQ);
    obj = MatchedObject(ci, scallingFactor);
    ci.setMinValue(obj);
  }

  final public void OptionalColumn(ColumnInfo ci, double scallingFactor) throws ParseException {
    label_3: while (true) {
      switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
      case MAXVAL:
      case MINVAL:
      case DISTINCTVAL:
        ;
        break;
      default:
        jj_la1[6] = jj_gen;
        break label_3;
      }
      switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
      case DISTINCTVAL:
        DistinctValues(ci, scallingFactor);
        break;
      case MINVAL:
        MinValue(ci, scallingFactor);
        break;
      case MAXVAL:
        MaxValue(ci, scallingFactor);
        break;
      default:
        jj_la1[7] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
    }
  }

  /** Reinitialise. */
  public void ReInit(java.io.InputStream stream) {
    ReInit(stream, null);
  }

  /** Reinitialise. */
  public void ReInit(java.io.InputStream stream, String encoding) {
    try {
      jj_input_stream.ReInit(stream, encoding, 1, 1);
    } catch (final java.io.UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 8; i++)
      jj_la1[i] = -1;
  }

  /** Reinitialise. */
  public void ReInit(java.io.Reader stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 8; i++)
      jj_la1[i] = -1;
  }

  /** Reinitialise. */
  public void ReInit(SchemaParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 8; i++)
      jj_la1[i] = -1;
  }

}
TOP

Related Classes of sql.schema.parser.SchemaParser$TableInfo

TOP
Copyright © 2018 www.massapi.com. 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.