Search in sources :

Example 11 with CalciteConnectionConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig in project beam by apache.

the class CalciteQueryPlanner method defaultConfig.

public FrameworkConfig defaultConfig(JdbcConnection connection, Collection<RuleSet> ruleSets) {
    final CalciteConnectionConfig config = connection.config();
    final SqlParser.ConfigBuilder parserConfig = SqlParser.configBuilder().setQuotedCasing(config.quotedCasing()).setUnquotedCasing(config.unquotedCasing()).setQuoting(config.quoting()).setConformance(config.conformance()).setCaseSensitive(config.caseSensitive());
    final SqlParserImplFactory parserFactory = config.parserFactory(SqlParserImplFactory.class, null);
    if (parserFactory != null) {
        parserConfig.setParserFactory(parserFactory);
    }
    final SchemaPlus schema = connection.getRootSchema();
    final SchemaPlus defaultSchema = connection.getCurrentSchemaPlus();
    final ImmutableList<RelTraitDef> traitDefs = ImmutableList.of(ConventionTraitDef.INSTANCE);
    final CalciteCatalogReader catalogReader = new CalciteCatalogReader(CalciteSchema.from(schema), ImmutableList.of(defaultSchema.getName()), connection.getTypeFactory(), connection.config());
    final SqlOperatorTable opTab0 = connection.config().fun(SqlOperatorTable.class, SqlStdOperatorTable.instance());
    return Frameworks.newConfigBuilder().parserConfig(parserConfig.build()).defaultSchema(defaultSchema).traitDefs(traitDefs).context(Contexts.of(connection.config())).ruleSets(ruleSets.toArray(new RuleSet[0])).costFactory(BeamCostModel.FACTORY).typeSystem(connection.getTypeFactory().getTypeSystem()).operatorTable(SqlOperatorTables.chain(opTab0, catalogReader)).build();
}
Also used : RuleSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet) CalciteCatalogReader(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.CalciteCatalogReader) CalciteConnectionConfig(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig) RelTraitDef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitDef) SqlOperatorTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable) SqlParser(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) SqlParserImplFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserImplFactory)

Example 12 with CalciteConnectionConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig in project calcite by apache.

the class PlannerImpl method createCatalogReader.

// CalciteCatalogReader is stateless; no need to store one
private CalciteCatalogReader createCatalogReader() {
    final SchemaPlus rootSchema = rootSchema(defaultSchema);
    final Context context = config.getContext();
    final CalciteConnectionConfig connectionConfig;
    if (context != null) {
        connectionConfig = context.unwrap(CalciteConnectionConfig.class);
    } else {
        Properties properties = new Properties();
        properties.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(), String.valueOf(parserConfig.caseSensitive()));
        connectionConfig = new CalciteConnectionConfigImpl(properties);
    }
    return new CalciteCatalogReader(CalciteSchema.from(rootSchema), CalciteSchema.from(defaultSchema).path(null), typeFactory, connectionConfig);
}
Also used : Context(org.apache.calcite.plan.Context) CalciteConnectionConfigImpl(org.apache.calcite.config.CalciteConnectionConfigImpl) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Properties(java.util.Properties)

Example 13 with CalciteConnectionConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig in project calcite by apache.

the class PsTableFunction method eval.

public static ScannableTable eval(boolean b) {
    return new ScannableTable() {

        public Enumerable<Object[]> scan(DataContext root) {
            final RelDataType rowType = getRowType(root.getTypeFactory());
            final List<String> fieldNames = ImmutableList.copyOf(rowType.getFieldNames());
            final String[] args;
            final String osName = System.getProperty("os.name");
            final String osVersion = System.getProperty("os.version");
            Util.discard(osVersion);
            switch(osName) {
                case // tested on version 10.12.5
                "Mac OS X":
                    args = new String[] { "ps", "ax", "-o", "ppid=,pid=,pgid=,tpgid=,stat=," + "user=,pcpu=,pmem=,vsz=,rss=,tty=,start=,time=,uid=,ruid=," + "sess=,comm=" };
                    break;
                default:
                    args = new String[] { "ps", "--no-headers", "axo", "ppid,pid,pgrp," + "tpgid,stat,user,pcpu,pmem,vsz,rss,tty,start_time,time,euid," + "ruid,sess,comm" };
            }
            return Processes.processLines(args).select(new Function1<String, Object[]>() {

                public Object[] apply(String line) {
                    final String[] fields = line.trim().split(" +");
                    final Object[] values = new Object[fieldNames.size()];
                    for (int i = 0; i < values.length; i++) {
                        try {
                            values[i] = field(fieldNames.get(i), fields[i]);
                        } catch (RuntimeException e) {
                            throw new RuntimeException("while parsing value [" + fields[i] + "] of field [" + fieldNames.get(i) + "] in line [" + line + "]");
                        }
                    }
                    return values;
                }

                private Object field(String field, String value) {
                    switch(field) {
                        case "pid":
                        case "ppid":
                        // linux only; macOS equivalent is "pgid"
                        case "pgrp":
                        // see "pgrp"
                        case "pgid":
                        case "tpgid":
                            return Integer.valueOf(value);
                        case "pcpu":
                        case "pmem":
                            return (int) (Float.valueOf(value) * 10f);
                        case "time":
                            final Matcher m1 = MINUTE_SECOND_MILLIS_PATTERN.matcher(value);
                            if (m1.matches()) {
                                final long h = Long.parseLong(m1.group(1));
                                final long m = Long.parseLong(m1.group(2));
                                final long s = Long.parseLong(m1.group(3));
                                return h * 3600000L + m * 60000L + s * 1000L;
                            }
                            final Matcher m2 = HOUR_MINUTE_SECOND_PATTERN.matcher(value);
                            if (m2.matches()) {
                                final long m = Long.parseLong(m2.group(1));
                                final long s = Long.parseLong(m2.group(2));
                                String g3 = m2.group(3);
                                while (g3.length() < 3) {
                                    g3 = g3 + "0";
                                }
                                final long millis = Long.parseLong(g3);
                                return m * 60000L + s * 1000L + millis;
                            }
                            return 0L;
                        // linux only; macOS version is "lstart"
                        case "start_time":
                        // see "start_time"
                        case "lstart":
                        // linux only; macOS equivalent is "uid"
                        case "euid":
                        // see "euid"
                        case "uid":
                        default:
                            return value;
                    }
                }
            });
        }

        public RelDataType getRowType(RelDataTypeFactory typeFactory) {
            return typeFactory.builder().add("pid", SqlTypeName.INTEGER).add("ppid", SqlTypeName.INTEGER).add("pgrp", SqlTypeName.INTEGER).add("tpgid", SqlTypeName.INTEGER).add("stat", SqlTypeName.VARCHAR).add("user", SqlTypeName.VARCHAR).add("pcpu", SqlTypeName.DECIMAL, 3, 1).add("pmem", SqlTypeName.DECIMAL, 3, 1).add("vsz", SqlTypeName.INTEGER).add("rss", SqlTypeName.INTEGER).add("tty", SqlTypeName.VARCHAR).add("start_time", SqlTypeName.VARCHAR).add("time", TimeUnit.HOUR, -1, TimeUnit.SECOND, 0).add("euid", SqlTypeName.VARCHAR).add("ruid", SqlTypeName.VARCHAR).add("sess", SqlTypeName.VARCHAR).add("command", SqlTypeName.VARCHAR).build();
        }

        public Statistic getStatistic() {
            return Statistics.of(1000d, ImmutableList.of(ImmutableBitSet.of(1)));
        }

        public Schema.TableType getJdbcTableType() {
            return Schema.TableType.TABLE;
        }

        public boolean isRolledUp(String column) {
            return false;
        }

        public boolean rolledUpColumnValidInsideAgg(String column, SqlCall call, SqlNode parent, CalciteConnectionConfig config) {
            return true;
        }
    };
}
Also used : Matcher(java.util.regex.Matcher) SqlCall(org.apache.calcite.sql.SqlCall) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) Schema(org.apache.calcite.schema.Schema) RelDataType(org.apache.calcite.rel.type.RelDataType) DataContext(org.apache.calcite.DataContext) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) ScannableTable(org.apache.calcite.schema.ScannableTable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 14 with CalciteConnectionConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig in project calcite by apache.

the class RelOptLattice method getAggregate.

/**
 * Retrieves a materialized table that will satisfy an aggregate query on
 * the star table.
 *
 * <p>The current implementation creates a materialization and populates it,
 * provided that {@link Lattice#auto} is true.
 *
 * <p>Future implementations might return materializations at a different
 * level of aggregation, from which the desired result can be obtained by
 * rolling up.
 *
 * @param planner Current planner
 * @param groupSet Grouping key
 * @param measureList Calls to aggregate functions
 * @return Materialized table
 */
public Pair<CalciteSchema.TableEntry, TileKey> getAggregate(RelOptPlanner planner, ImmutableBitSet groupSet, List<Lattice.Measure> measureList) {
    final CalciteConnectionConfig config = planner.getContext().unwrap(CalciteConnectionConfig.class);
    if (config == null) {
        return null;
    }
    final MaterializationService service = MaterializationService.instance();
    boolean create = lattice.auto && config.createMaterializations();
    final CalciteSchema schema = starRelOptTable.unwrap(CalciteSchema.class);
    return service.defineTile(lattice, groupSet, measureList, schema, create, false);
}
Also used : CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) MaterializationService(org.apache.calcite.materialize.MaterializationService)

Example 15 with CalciteConnectionConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig in project calcite by apache.

the class DruidQueryFilterTest method testSetup.

@Before
public void testSetup() {
    druidQuery = Mockito.mock(DruidQuery.class);
    final CalciteConnectionConfig connectionConfigMock = Mockito.mock(CalciteConnectionConfig.class);
    Mockito.when(connectionConfigMock.timeZone()).thenReturn("UTC");
    Mockito.when(druidQuery.getConnectionConfig()).thenReturn(connectionConfigMock);
    Mockito.when(druidQuery.getDruidTable()).thenReturn(new DruidTable(Mockito.mock(DruidSchema.class), "dataSource", null, ImmutableSet.<String>of(), "timestamp", null, null, null));
}
Also used : CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) Before(org.junit.Before)

Aggregations

CalciteConnectionConfig (org.apache.calcite.config.CalciteConnectionConfig)14 RelDataType (org.apache.calcite.rel.type.RelDataType)5 SqlNode (org.apache.calcite.sql.SqlNode)5 Properties (java.util.Properties)4 DataContext (org.apache.calcite.DataContext)4 CalciteConnectionConfigImpl (org.apache.calcite.config.CalciteConnectionConfigImpl)4 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)4 ScannableTable (org.apache.calcite.schema.ScannableTable)4 Schema (org.apache.calcite.schema.Schema)4 SqlCall (org.apache.calcite.sql.SqlCall)4 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CalciteConnectionConfig (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig)2 RelTraitDef (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitDef)2 CalciteCatalogReader (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.CalciteCatalogReader)2 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)2 SqlOperatorTable (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable)2 SqlParser (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser)2 SqlParserImplFactory (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserImplFactory)2