SQLAnalyzer extends SQLParserBaseVisitor<Expr> { private Map<String, String> getParams(SQLParser.Param_clauseContext ctx) { Map<String, String> params = new HashMap<String, String>(); for (int i = 0; i < ctx.param().size(); i++) { params.put(stripQuote(ctx.param(i).key.getText()), stripQuote(ctx.param(i).value.getText())); } return params; } SQLAnalyzer(); Expr parse(String sql); @Override Expr visitSql(SqlContext ctx); Expr visitSession_statement(@NotNull SQLParser.Session_statementContext ctx); @Override Expr visitNon_join_query_expression(SQLParser.Non_join_query_expressionContext ctx); @Override Expr visitNon_join_query_term(Non_join_query_termContext ctx); @Override Expr visitNon_join_query_primary(Non_join_query_primaryContext ctx); @Override Expr visitQuery_specification(SQLParser.Query_specificationContext ctx); @Override Projection visitSelect_list(SQLParser.Select_listContext ctx); @Override NamedExpr visitSelect_sublist(SQLParser.Select_sublistContext ctx); @Override RelationList visitFrom_clause(SQLParser.From_clauseContext ctx); @Override Selection visitWhere_clause(SQLParser.Where_clauseContext ctx); @Override Aggregation visitGroupby_clause(SQLParser.Groupby_clauseContext ctx); @Override WindowFunctionExpr visitWindow_function(@NotNull SQLParser.Window_functionContext context); @Override Window visitWindow_clause(@NotNull SQLParser.Window_clauseContext ctx); WindowSpec buildWindowSpec(SQLParser.Window_specificationContext ctx); WindowSpec.WindowStartBound buildWindowStartBound(Window_frame_start_boundContext context); WindowSpec.WindowEndBound buildWindowEndBound(Window_frame_end_boundContext context); Sort.SortSpec[] buildSortSpecs(Sort_specifier_listContext context); @Override Sort visitOrderby_clause(SQLParser.Orderby_clauseContext ctx); @Override Limit visitLimit_clause(SQLParser.Limit_clauseContext ctx); @Override Expr visitJoined_table(SQLParser.Joined_tableContext ctx); @Override Join visitJoined_table_primary(SQLParser.Joined_table_primaryContext ctx); @Override Expr visitTable_primary(SQLParser.Table_primaryContext ctx); @Override Expr visitSubquery(SQLParser.SubqueryContext ctx); @Override BetweenPredicate visitBetween_predicate(SQLParser.Between_predicateContext ctx); @Override CaseWhenPredicate visitSimple_case(SQLParser.Simple_caseContext ctx); @Override CaseWhenPredicate visitSearched_case(SQLParser.Searched_caseContext ctx); @Override Expr visitCommon_value_expression(SQLParser.Common_value_expressionContext ctx); @Override Expr visitParenthesized_value_expression(SQLParser.Parenthesized_value_expressionContext ctx); @Override Expr visitBoolean_value_expression(SQLParser.Boolean_value_expressionContext ctx); @Override Expr visitOr_predicate(SQLParser.Or_predicateContext ctx); @Override Expr visitAnd_predicate(SQLParser.And_predicateContext ctx); @Override Expr visitBoolean_factor(SQLParser.Boolean_factorContext ctx); @Override Expr visitBoolean_test(SQLParser.Boolean_testContext ctx); @Override Expr visitBoolean_primary(SQLParser.Boolean_primaryContext ctx); @Override Expr visitBoolean_predicand(SQLParser.Boolean_predicandContext ctx); @Override Expr visitNonparenthesized_value_expression_primary(
      SQLParser.Nonparenthesized_value_expression_primaryContext ctx); @Override Expr visitRow_value_predicand(SQLParser.Row_value_predicandContext ctx); @Override Expr visitRow_value_constructor_predicand(SQLParser.Row_value_constructor_predicandContext ctx); @Override BinaryOperator visitComparison_predicate(SQLParser.Comparison_predicateContext ctx); @Override Expr visitNumeric_value_expression(SQLParser.Numeric_value_expressionContext ctx); @Override Expr visitTerm(SQLParser.TermContext ctx); @Override Expr visitFactor(SQLParser.FactorContext ctx); @Override Expr visitNumeric_primary(SQLParser.Numeric_primaryContext ctx); static OpType tokenToExprType(int tokenId); @Override InPredicate visitIn_predicate(SQLParser.In_predicateContext ctx); @Override Expr visitIn_predicate_value(SQLParser.In_predicate_valueContext ctx); @Override Expr visitArray(SQLParser.ArrayContext ctx); @Override Expr visitPattern_matching_predicate(SQLParser.Pattern_matching_predicateContext ctx); @Override IsNullPredicate visitNull_predicate(SQLParser.Null_predicateContext ctx); @Override ExistsPredicate visitExists_predicate(SQLParser.Exists_predicateContext ctx); @Override ColumnReferenceExpr visitColumn_reference(SQLParser.Column_referenceContext ctx); @Override LiteralValue visitUnsigned_numeric_literal(@NotNull SQLParser.Unsigned_numeric_literalContext ctx); @Override GeneralSetFunctionExpr visitAggregate_function(SQLParser.Aggregate_functionContext ctx); @Override GeneralSetFunctionExpr visitGeneral_set_function(SQLParser.General_set_functionContext ctx); @Override FunctionExpr visitRoutine_invocation(SQLParser.Routine_invocationContext ctx); @Override NamedExpr visitDerived_column(Derived_columnContext ctx); @Override NamedExpr visitQualified_asterisk(Qualified_asteriskContext ctx); @Override Expr visitCharacter_string_type(SQLParser.Character_string_typeContext ctx); @Override Expr visitCharacter_value_expression(SQLParser.Character_value_expressionContext ctx); @Override Expr visitNumeric_value_function(Numeric_value_functionContext ctx); @Override Expr visitExtract_expression(Extract_expressionContext ctx); @Override Expr visitTrim_function(SQLParser.Trim_functionContext ctx); @Override Expr visitDatabase_definition(@NotNull SQLParser.Database_definitionContext ctx); @Override Expr visitDrop_database_statement(@NotNull SQLParser.Drop_database_statementContext ctx); @Override Expr visitCreate_table_statement(SQLParser.Create_table_statementContext ctx); @Override Expr visitTruncate_table_statement(@NotNull SQLParser.Truncate_table_statementContext ctx); PartitionMethodDescExpr parseTablePartitioningClause(SQLParser.Table_partitioning_clausesContext ctx); @Override DataTypeExpr visitData_type(SQLParser.Data_typeContext ctx); @Override Expr visitInsert_statement(SQLParser.Insert_statementContext ctx); @Override Expr visitDrop_table_statement(SQLParser.Drop_table_statementContext ctx); Map<String, String> escapeTableMeta(Map<String, String> map); @Override Expr visitCast_specification(SQLParser.Cast_specificationContext ctx); @Override Expr visitUnsigned_value_specification(@NotNull SQLParser.Unsigned_value_specificationContext ctx); @Override Expr visitUnsigned_literal(@NotNull SQLParser.Unsigned_literalContext ctx); @Override Expr visitGeneral_literal(SQLParser.General_literalContext ctx); @Override Expr visitDatetime_literal(@NotNull SQLParser.Datetime_literalContext ctx); @Override Expr visitTime_literal(SQLParser.Time_literalContext ctx); @Override Expr visitDate_literal(SQLParser.Date_literalContext ctx); @Override Expr visitTimestamp_literal(SQLParser.Timestamp_literalContext ctx); @Override Expr visitInterval_literal(@NotNull SQLParser.Interval_literalContext ctx); @Override Expr visitDatetime_value_expression(@NotNull SQLParser.Datetime_value_expressionContext ctx); @Override Expr visitDatetime_term(@NotNull SQLParser.Datetime_termContext ctx); @Override Expr visitDatetime_factor(@NotNull SQLParser.Datetime_factorContext ctx); @Override Expr visitDatetime_primary(@NotNull SQLParser.Datetime_primaryContext ctx); @Override Expr visitDatetime_value_function(@NotNull SQLParser.Datetime_value_functionContext ctx); @Override Expr visitCurrent_date_value_function(@NotNull SQLParser.Current_date_value_functionContext ctx); @Override Expr visitCurrent_time_value_function(@NotNull SQLParser.Current_time_value_functionContext ctx); @Override Expr visitCurrent_timestamp_value_function(@NotNull SQLParser.Current_timestamp_value_functionContext ctx); @Override Expr visitAlter_tablespace_statement(@NotNull SQLParser.Alter_tablespace_statementContext ctx); @Override Expr visitAlter_table_statement(SQLParser.Alter_table_statementContext ctx);  }
@Test public void testAlterTableSetProperty1() throws IOException { String sql = FileUtil.readTextFileFromResource("queries/default/alter_table_set_property_1.sql"); Expr expr = parseQuery(sql); assertEquals(OpType.AlterTable, expr.getType()); AlterTable alterTable = (AlterTable)expr; assertEquals(alterTable.getAlterTableOpType(), AlterTableOpType.SET_PROPERTY); assertTrue(alterTable.hasParams()); assertTrue(alterTable.getParams().containsKey("timezone")); assertEquals("GMT-7", alterTable.getParams().get("timezone")); }
@Test public void testAlterTableSetProperty2() throws IOException { String sql = FileUtil.readTextFileFromResource("queries/default/alter_table_set_property_2.sql"); Expr expr = parseQuery(sql); assertEquals(OpType.AlterTable, expr.getType()); AlterTable alterTable = (AlterTable)expr; assertEquals(alterTable.getAlterTableOpType(), AlterTableOpType.SET_PROPERTY); assertTrue(alterTable.hasParams()); assertTrue(alterTable.getParams().containsKey("text.delimiter")); assertEquals("&", alterTable.getParams().get("text.delimiter")); }
@Test public void testAlterTableSetProperty3() throws IOException { String sql = FileUtil.readTextFileFromResource("queries/default/alter_table_set_property_3.sql"); Expr expr = parseQuery(sql); assertEquals(OpType.AlterTable, expr.getType()); AlterTable alterTable = (AlterTable)expr; assertEquals(alterTable.getAlterTableOpType(), AlterTableOpType.SET_PROPERTY); assertTrue(alterTable.hasParams()); assertTrue(alterTable.getParams().containsKey("compression.type")); assertEquals("RECORD", alterTable.getParams().get("compression.type")); assertTrue(alterTable.getParams().containsKey("compression.codec")); assertEquals("org.apache.hadoop.io.compress.SnappyCodec", alterTable.getParams().get("compression.codec")); }