private static TableDesc createTableDesc(CreateTableNode createTableNode) { TableMeta meta = new TableMeta(createTableNode.getStorageType(), createTableNode.getOptions()); TableDesc tableDescTobeCreated = new TableDesc( createTableNode.getTableName(), createTableNode.getTableSchema(), meta, createTableNode.getUri() != null ? createTableNode.getUri() : null); tableDescTobeCreated.setExternal(createTableNode.isExternal()); if (createTableNode.hasPartition()) { tableDescTobeCreated.setPartitionMethod(createTableNode.getPartitionMethod()); } return tableDescTobeCreated; }
public void prepareTable(LogicalNode node) throws TajoException, IOException { if (node.getType() == NodeType.CREATE_TABLE) { CreateTableNode cNode = (CreateTableNode)node; if (!cNode.isExternal()) { TableMeta tableMeta = new TableMeta(cNode.getStorageType(), cNode.getOptions()); createTable( ((CreateTableNode) node).getUri(), tableMeta, cNode.getTableSchema(), cNode.isExternal(), cNode.isIfNotExists()); } } }
@Override public LogicalNode visitCreateTable(Context context, LogicalPlan plan, LogicalPlan.QueryBlock block, CreateTableNode node, Stack<LogicalNode> stack) throws TajoException { super.visitCreateTable(context, plan, block, node, stack); // here, we don't need check table existence because this check is performed in PreLogicalPlanVerifier. if (node.hasSubQuery()) { ensureDomains(context.state, node.getLogicalSchema(), node.getChild(0).getOutSchema()); } return node; }
public TableDesc create(QueryContext queryContext, CreateTableNode createTable, boolean ifNotExists) throws IOException, TajoException { TableMeta meta; if (createTable.hasOptions()) { meta = CatalogUtil.newTableMeta(createTable.getStorageType(), createTable.getOptions()); } else { meta = CatalogUtil.newTableMeta(createTable.getStorageType(), queryContext.getConf()); } if(PlannerUtil.isFileStorageType(createTable.getStorageType()) && createTable.isExternal()){ Preconditions.checkState(createTable.hasUri(), "ERROR: LOCATION must be given."); } return create( queryContext, createTable.getTableName(), createTable.getTableSpaceName(), createTable.getTableSchema(), meta, createTable.getUri(), createTable.isExternal(), createTable.getPartitionMethod(), ifNotExists); }
createTableNode.setIfNotExists(expr.isIfNotExists()); createTableNode.setTableName(expr.getTableName()); } else { createTableNode.setTableName( CatalogUtil.buildFQName(context.queryContext.get(SessionVars.CURRENT_DATABASE), expr.getTableName())); createTableNode.setTableSpaceName(expr.getTableSpaceName()); createTableNode.setUri(getCreatedTableURI(context, expr)); createTableNode.setDataFormat(CatalogUtil.getBackwardCompitableDataFormat(expr.getStorageType())); } else { // otherwise, default type createTableNode.setDataFormat(BuiltinStorages.TEXT); createTableNode.setOptions( CatalogUtil.newDefaultProperty(createTableNode.getStorageType(), context.getQueryContext().getConf())); createTableNode.getOptions().putAll(expr.getParams()); createTableNode.setPartitionMethod(getPartitionMethod(context, expr.getTableName(), expr.getPartitionMethod())); } else { throw ExceptionUtil.makeNotSupported( LogicalNode subQuery = visit(context, stack, expr.getSubQuery()); stack.pop(); createTableNode.setChild(subQuery); createTableNode.setInSchema(subQuery.getOutSchema());
@Test public final void testCreateTableDef() throws TajoException { QueryContext qc = createQueryContext(); Expr expr = sqlAnalyzer.parse(CREATE_TABLE[0]); LogicalNode plan = planner.createPlan(qc, expr).getRootBlock().getRoot(); LogicalRootNode root = (LogicalRootNode) plan; assertEquals(NodeType.CREATE_TABLE, root.getChild().getType()); CreateTableNode createTable = root.getChild(); Schema def = createTable.getTableSchema(); assertEquals("name", def.getColumn(0).getSimpleName()); assertEquals(Type.TEXT, def.getColumn(0).getDataType().getType()); assertEquals("age", def.getColumn(1).getSimpleName()); assertEquals(Type.INT4, def.getColumn(1).getDataType().getType()); assertEquals("earn", def.getColumn(2).getSimpleName()); assertEquals(Type.INT8, def.getColumn(2).getDataType().getType()); assertEquals("score", def.getColumn(3).getSimpleName()); assertEquals(Type.FLOAT4, def.getColumn(3).getDataType().getType()); assertTrue("TEXT".equalsIgnoreCase(createTable.getStorageType())); assertEquals("file://tmp/data", createTable.getUri().toString()); assertTrue(createTable.hasOptions()); assertEquals("\\u007c", createTable.getOptions().get("text.delimiter")); }
@Override public void rollbackTable(LogicalNode node) throws IOException, TajoException { if (node.getType() == NodeType.CREATE_TABLE) { CreateTableNode cNode = (CreateTableNode)node; if (cNode.isExternal()) { return; } TableMeta tableMeta = new TableMeta(cNode.getStorageType(), cNode.getOptions()); try (HBaseAdmin hAdmin = new HBaseAdmin(this.hbaseConf)) { HTableDescriptor hTableDesc = parseHTableDescriptor(tableMeta, cNode.getTableSchema()); LOG.info("Delete table cause query failed:" + new String(hTableDesc.getName())); hAdmin.disableTable(hTableDesc.getName()); hAdmin.deleteTable(hTableDesc.getName()); } } }
@Override public LogicalNode visitCreateTable(SerializeContext context, LogicalPlan plan, LogicalPlan.QueryBlock block, CreateTableNode node, Stack<LogicalNode> stack) throws TajoException { super.visitCreateTable(context, plan, block, node, stack); int [] childIds = registerGetChildIds(context, node); PlanProto.PersistentStoreNode.Builder persistentStoreBuilder = buildPersistentStoreBuilder(node, childIds); PlanProto.StoreTableNodeSpec.Builder storeTableBuilder = buildStoreTableNodeSpec(node); PlanProto.CreateTableNodeSpec.Builder createTableBuilder = PlanProto.CreateTableNodeSpec.newBuilder(); if (node.hasTableSpaceName()) { createTableBuilder.setTablespaceName(node.getTableSpaceName()); } createTableBuilder.setExternal(node.isExternal()); createTableBuilder.setIfNotExists(node.isIfNotExists()); PlanProto.LogicalNode.Builder nodeBuilder = createNodeBuilder(context, node); nodeBuilder.setPersistentStore(persistentStoreBuilder); nodeBuilder.setStoreTable(storeTableBuilder); nodeBuilder.setCreateTable(createTableBuilder); context.treeBuilder.addNodes(nodeBuilder); return node; }
/** * It sets default table property for newly created table * * @param context QueryContext * @param node CreateTableNode */ public static void setTableProperty(OverridableConf context, CreateTableNode node) { String dataFormat = node.getStorageType(); KeyValueSet property = node.getOptions(); if (dataFormat.equalsIgnoreCase("TEXT") || dataFormat.equalsIgnoreCase("TEXT")) { setSessionToProperty(context, SessionVars.NULL_CHAR, property, StorageConstants.TEXT_NULL); } }
@Override public void hook(QueryContext queryContext, LogicalPlan plan) throws Exception { LogicalRootNode rootNode = plan.getRootBlock().getRoot(); CreateTableNode createTableNode = rootNode.getChild(); String [] splitted = IdentifierUtil.splitFQTableName(createTableNode.getTableName()); String tableName = splitted[1]; queryContext.setOutputTable(tableName); // set the final output table uri queryContext.setOutputPath(createTableNode.getUri()); if(createTableNode.getPartitionMethod() != null) { queryContext.setPartitionMethod(createTableNode.getPartitionMethod()); } queryContext.setCreateTable(); } }
/** * Check if CTAS is already done * @param rootNode * @return */ private boolean checkIfCtasAlreadyDone(LogicalNode rootNode) { if (rootNode.getChild(0).getType() == NodeType.CREATE_TABLE) { CreateTableNode createTable = (CreateTableNode) rootNode.getChild(0); if (createTable.isIfNotExists() && catalog.existsTable(createTable.getTableName())) { return true; } } return false; }
public Schema getLogicalSchema() { if (hasPartition()) { Schema logicalSchema = new Schema(tableSchema); logicalSchema.addColumns(getPartitionMethod().getExpressionSchema()); return logicalSchema; } else { return tableSchema; } }
public static String getDataFormat(LogicalPlan plan) { LogicalRootNode rootNode = plan.getRootBlock().getRoot(); NodeType nodeType = rootNode.getChild().getType(); if (nodeType == NodeType.CREATE_TABLE) { return ((CreateTableNode)rootNode.getChild()).getStorageType(); } else if (nodeType == NodeType.INSERT) { return ((InsertNode)rootNode.getChild()).getStorageType(); } else { return null; } }
createTableExecutor.create(queryContext, createTable, createTable.isIfNotExists()); return true; case DROP_TABLE:
@Override public LogicalNode visitCreateTable(GlobalPlanContext context, LogicalPlan plan, LogicalPlan.QueryBlock queryBlock, CreateTableNode node, Stack<LogicalNode> stack) throws TajoException { LogicalNode child = super.visitStoreTable(context, plan, queryBlock, node, stack); ExecutionBlock childBlock = context.execBlockMap.remove(child.getPID()); ExecutionBlock newExecBlock = buildStorePlan(context, childBlock, node); context.execBlockMap.put(node.getPID(), newExecBlock); return node; }
public static String getStoreTableName(LogicalPlan plan) { LogicalRootNode rootNode = plan.getRootBlock().getRoot(); NodeType nodeType = rootNode.getChild().getType(); if (nodeType == NodeType.CREATE_TABLE) { return ((CreateTableNode)rootNode.getChild()).getTableName(); } else if (nodeType == NodeType.INSERT) { return ((InsertNode)rootNode.getChild()).getTableName(); } else { return null; } }
} else if (this.plan.getType() == NodeType.CREATE_TABLE) { CreateTableNode createTable = (CreateTableNode) plan; int idx = createTable.getLogicalSchema().getColumnId(column.getQualifiedName()); keyIds[i] = idx; } else {
public TableDesc create(QueryContext queryContext, CreateTableNode createTable, boolean ifNotExists) throws IOException, TajoException { TableMeta meta; if (createTable.hasOptions()) { meta = CatalogUtil.newTableMeta(createTable.getStorageType(), createTable.getOptions()); } else { meta = CatalogUtil.newTableMeta(createTable.getStorageType(), queryContext.getConf()); } if(PlannerUtil.isFileStorageType(createTable.getStorageType()) && createTable.isExternal()){ Preconditions.checkState(createTable.hasUri(), "ERROR: LOCATION must be given."); } return create( queryContext, createTable.getTableName(), createTable.getTableSpaceName(), createTable.getTableSchema(), meta, createTable.getUri(), createTable.isExternal(), createTable.getPartitionMethod(), ifNotExists); }
createTableNode.setIfNotExists(expr.isIfNotExists()); createTableNode.setTableName(expr.getTableName()); } else { createTableNode.setTableName( IdentifierUtil.buildFQName(context.queryContext.get(SessionVars.CURRENT_DATABASE), expr.getTableName())); createTableNode.setTableSpaceName(expr.getTableSpaceName()); createTableNode.setUri(getCreatedTableURI(context, expr)); createTableNode.setDataFormat(CatalogUtil.getBackwardCompitableDataFormat(expr.getStorageType())); } else { // otherwise, default type createTableNode.setDataFormat(BuiltinStorages.TEXT); createTableNode.setOptions( CatalogUtil.newDefaultProperty(createTableNode.getStorageType(), context.getQueryContext().getConf())); createTableNode.getOptions().putAll(expr.getParams()); createTableNode.setPartitionMethod(getPartitionMethod(context, expr.getTableName(), expr.getPartitionMethod())); } else { throw ExceptionUtil.makeNotSupported( LogicalNode subQuery = visit(context, stack, expr.getSubQuery()); stack.pop(); createTableNode.setChild(subQuery); createTableNode.setInSchema(subQuery.getOutSchema());
@Override public void rollbackTable(LogicalNode node) throws IOException, TajoException { if (node.getType() == NodeType.CREATE_TABLE) { CreateTableNode cNode = (CreateTableNode)node; if (cNode.isExternal()) { return; } HBaseAdmin hAdmin = new HBaseAdmin(this.hbaseConf); TableMeta tableMeta = new TableMeta(cNode.getStorageType(), cNode.getOptions()); try { HTableDescriptor hTableDesc = parseHTableDescriptor(tableMeta, cNode.getTableSchema()); LOG.info("Delete table cause query failed:" + new String(hTableDesc.getName())); hAdmin.disableTable(hTableDesc.getName()); hAdmin.deleteTable(hTableDesc.getName()); } finally { hAdmin.close(); } } }